Is it possible to share states between components using the useState() hook in React?

后端 未结 7 1366
春和景丽
春和景丽 2020-12-13 00:19

I was experimenting with the new Hook feature in React. Considering I have the following two components (using React Hooks) -

const HookComponent = () =>         


        
7条回答
  •  被撕碎了的回忆
    2020-12-13 00:35

    If you are referring to component state, then hooks will not help you share it between components. Component state is local to the component. If your state lives in context, then useContext hook would be helpful.

    Fundamentally, I think you misunderstood the line "sharing stateful logic between components". Stateful logic is different from state. Stateful logic is stuff that you do that modifies state. For e.g., a component subscribing to a store in componentDidMount() and unsubscribing in componentWillUnmount(). This subscribing/unsubscribing behavior can be implemented in a hook and components which need this behavior can just use the hook.

    If you want to share state between components, there are various ways to do so, each with its own merits:

    1. Lift State Up

    Lift state up to a common ancestor component of the two components.

    function Ancestor() {
        const [count, setCount] = useState(999);
        return <>
          
          
        ;
      }
    

    This state sharing approach is not fundamentally different from the traditional way of using state, hooks just give us a different way to declare component state.

    2. Context

    If the descendants are too deep down in the component hierarchy and you don't want to pass the state down too many layers, you could use the Context API.

    There's a useContext hook which you can leverage on within the child components.

    3. External State Management Solution

    State management libraries like Redux or Mobx. Your state will then live in a store outside of React and components can connect/subscribe to the store to receive updates.

提交回复
热议问题