react-redux-starter-kit icon indicating copy to clipboard operation
react-redux-starter-kit copied to clipboard

How do I inject a store into a reusable component?

Open sentry0 opened this issue 8 years ago • 2 comments

I have a Header component in the reusable component directory that I need to bind props into. I've added a container to the reusable container directory but it doesn't seem to be binding my props.

My directory structure looks like this: src |-components |----Header |--------Header.js |-containers |----HeaderContainer.js

sentry0 avatar Jun 19 '17 20:06 sentry0

So I'm not sure what the point of the "containers" directory is at this point, my HeaderContainer.js file was being ignored. I ended up putting the connect code in the header component itself which got me to where I needed to be.

class _Header extends Component {
. . .
}

const mapStateToProps = (state) => ({
	  userDetails: state.userDetails
})

const Header = connect(mapStateToProps, {setUserDetails})(_Header);

export default Header;

sentry0 avatar Jun 23 '17 13:06 sentry0

That's hardly a good solution, you can just pass your desired props from the top container, which I assume already has the props binded, it should look like so:

<Header details={props.details} />

Seeing how you pass a explicit value userDetails to connect, made me think that this Header can be hardly reusable, as it only seems to do a very explicit role, which is to render userDetails..

joeyhipolito avatar Jul 02 '17 22:07 joeyhipolito