简体   繁体   中英

Component gets data from its parent or directly from Redux store

I have the following component:

const CompA = () => {
  const user = useSelector(state => state.user);
  // ... the rest

  return (
    <div>
      <CompB />
    </div>
  );
}

This component uses the user data from Redux store. And I also have the CompB component that is a child of the CompA component, this component uses the user data from Redux store, too. There are two ways for the CompB to get the data:

  • Gets the data from its parent through props or context .
  • Gets the data directly from the Redux store by using useSelector hook (like its parent).

I'm struggling to choose one from those approaches. Which one is better and why? Thank you.

If <CompB /> is a dump component and he shouldn't know anything about enviroment and business logic the best option would be to do it with props . Otherwise, use useSelector in <CompB /> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM