Code Monkey home page Code Monkey logo

Comments (1)

diegohaz avatar diegohaz commented on May 20, 2024

Hi, @goncy.

I have no plans to provide a HOC on this library, unless a lot of people actually demand it. I suggest you and other people interested on this topic to watch this talk by @mjackson: Never Write Another HoC.

That said, this is how a HOC implementation of State could look like:

const withState = stateProps => Component => props => (
  <State {...stateProps}>
    {state => <Component {...props} state={state} />}
  </State>
);

const enhance = withState({
  initialState: { count: 0 },
  actions: {
    increment: amount => state => ({ count: state.count + amount })
  },
  context: 'counter1'
});

const CounterValue = enhance(({ state }) => (
  <div>{state.count}</div>
));

One of the drawbacks is that you need to pass stateProps statically to withState. You can't pass context from within CounterValue for example.

I recommend you to stick with render props and use one of the following approaches to access state in React lifecycles:

  1. Create another component:
class Component extends React.Component {
  componentDidMount() {
    this.props.increment(1);
  }

  render() {
    return <div>{this.props.count}</div>;
  }
}

const Counter = () => (
  <CounterState context="counter1">
    {state => <Component {...state} />}
  </CounterState>
);
  1. Use https://github.com/reactions/component:
import Component from "@reactions/component";

const Counter = () => (
  <CounterState context="counter1">
    {({ count, increment }) => (
      <Component didMount={() => increment(1)}>
        <div>{count}</div>
      </Component>
    )}
  </CounterState>
);

I'm going to close this issue for now. But I can reopen in the future if people really want HOCs.

from constate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.