简体   繁体   中英

React & Redux - container component inside a presentational component?

I know this might be a little opinion-based question, but I think an important one.

In short, is it good practice to have react container components inside a react presentational component? What would you say could be the benefits and disadvantages of this practice of having container components inside a react presentational component?

I think that presentational components should be dummy: They take in props, and behave according to them, and nothing else affects them. They behave expectedly when given certain props, and nothing outwhere should affect their behaviour.

What could be the problem then having container component(s) inside a presentational component? Well, as container components usually have access to the (global) store (in redux), you cannot know simply from the props of the presentational component, that how the component will behave. Because the store will affect to the behaviour of the container component, and as it is included in the presentational component, then the presentational component is no longer a dummy component. What do you think?

I assume that by presentational component you are referring to a ReactJS functional component?

If so, I do not see a problem with this approach. The resulting component wouldn't be purely presentational, but it would do the job of defining the composition of the component(s) it renders.

Depending on the context, this could be exactly regards you need.

Rather than trying to build an application starting with presentational components, I prefer to take the approach of first scaffolding the bulk of my application and then refactor what I need to into presentational components.

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