Skip to content

Instantly share code, notes, and snippets.

@elrumordelaluz
Last active February 4, 2016 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elrumordelaluz/9922ce0bef76f50fbfb1 to your computer and use it in GitHub Desktop.
Save elrumordelaluz/9922ce0bef76f50fbfb1 to your computer and use it in GitHub Desktop.
/***
*
* Is it ok to declare and use both `mapStateToChildListProps` and `mapStateToParentListProps`
* or there is a way to unify. I 'am thinkin if need more `connect()`ed comps will add more `mapStateTo__Props`.
*
***/
const Child = ({ ...props }) => (
<li>
<h3>{ props.foo }</h3>
<p>{ props.bar }</p>
</li>
);
const ChildList = ({ nodes }) => (
<ul>
{Object.keys(nodes).map((node) => <Child {...nodes[node]} />)}
</ul>
);
const mapStateToChildListProps = (state, ownProps) => {
return {
nodes: state.parents[ownProps.child].nodes
};
};
const mapDispatchToChildListProps = (dispatch, ownProps) => {
return {
onChildClick: () => {
// ...
}
};
};
const FilteredChildList = connect(
mapStateToChildListProps,
mapDispatchToChildListProps
)(ChildList);
const Parent = ({ ...props }) => (
<li>
<h3>{ props.name }</h3>
<FilteredChildList child={props.child} />
</li>
);
const ParentList = ({ parents }) => (
<ul>
{Object.keys(parents).map((parent) => <Parent {...parents[parent]} />)}
</ul>
);
const mapStateToParentListProps = (state, ownProps) => {
return {
parents: state.parents
};
};
const mapDispatchToParentListProps = (dispatch, ownProps) => {
return {
onSetClick: () => {
// ...
}
};
};
const FilteredParenttList = connect(
mapStateToParentListProps,
mapDispatchToParentListProps
)(ParentList);
const App = () => <FilteredParenttList />;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment