react-state-rxjs
  • Introduction
  • Main differences
  • Performance first
  • GETTING STARTED
    • Instalation
    • Examples
  • Core concepts
    • Main idea
      • More complex flow visualization
    • Configuration
    • Store
      • Operators
      • Optimistic updates plugin
      • Form manager plugin
        • ShouldUpdateState hook
        • Custom form elements
      • Persist state plugin
        • Configuring custom storage
    • Actions
      • Immer
      • ImutableJs
      • Async
    • Components with Actions
    • @InjectStore decorator
    • @ComponentState decorator
    • ComponentState Hook
    • Dispatcher
    • Router / History
  • DIFFERENT SCENARIOS
    • Passing list item index via router
  • UNIT TESTING
    • Setup
    • Test store
    • Test actions
    • Test component with actions
    • Test with Enzyme
  • Debugging
    • Setup
    • Redux DevTools
    • Automated changes output
    • Manual state changes check
    • Additional debugging information
  • Production
    • Production mode
  • Other information
    • Best practices
    • CLI
      • Custom Configurations
    • Performance
    • Blog Posts
    • Contributing
Powered by GitBook
On this page

Was this helpful?

  1. Core concepts

ComponentState Hook

Is used to create actions

Previous@ComponentState decoratorNextDispatcher

Last updated 5 years ago

Was this helpful?

@react-state follows React and supports hooks from version 7.0.0.

In order to cretae actions you need to pass followinf parameters to useComponentState hook

  1. Actions type

  2. statePath (optional - if not passed then takes root)

  3. stateIndex (optional)

Hook returns actions and new statePath which can be passed further.

const TodoDescriptionHooks = ({ statePath, stateIndex }) => {
    const { actions, statePath } = useComponentState(TodoStateActions, statePath, stateIndex);

    return (<div>{ actions.testTodoDescription }</div>);
};

export default React.memo(TodoDescriptionHooks);

In decorator, when using classes, there is extended shouldComponentUpdate method which makes sure that component is not rerendered until state is changed. Since hooks do not have shouldComponentUpdate lifecycle method, React proposes to use React.memo in places where it is needed. So in this I strongly advice users to pay more attention and use hooks to according to best React practices because it might affect overall performance.

Hooks allows to use multiple actions in one component. If you need to to this it is first sign of state architecture smell and is going against @react-state ideology that -

@ComponentState
state is representation of your components on the screen