Similar to the ToDo example for Redux, my project structure is similar - just a container with an array of child components to display. The store would look something like the following:
{
workspace: {
widgets: [1, 2, 3]
}
widgets: {
1: {
id: 1,
title: 'First Widget',
lastPrice: 123.324,
lastUpdate: '2015-11-12'
},
2: {
id: 2,
title: 'Second Widget',
lastPrice: 1.624,
lastUpdate: '2015-11-12'
},
3: {
id: 3,
title: 'Third Widget',
lastPrice: 4.345,
lastUpdate: '2015-11-12'
}
}
}
Each widget item is a complex component in it's own right - lots of possible actions, many properties, and many child dumb components. Hence, I've made each widget a connected smart component with a dedicated WidgetReducer to keep those state changes separate.
I'd like the WidgetReducer to be concerned with just one widget at a time, so the state handled by it would just be a single widget node like:
{ id: 3,
title: 'Third Widget',
lastPrice: 4.345,
lastUpdate: '2015-11-12'
}
My Workspace component currently iterates over all Widgets, passing the Widget ID to each Widget smart component:
@connect(state => ({ workspace: state.workspace }))
export default class Workspace extends React.Component {
render() {
return (
<div>
{this.props.workspace.widgets.map(id =>
(<Widget key={id} widgetId={id} />)
)}
</div>
);
}
}
My Widget component is like so:
@connect((state, ownProps) => ({ widget: state.widgets[ownProps.widgetId]}))
export default class Widget extends React.Component {
componentWillMount() {
this.props.dispatch(subscribeToUpdates(this.props.widgetId));
}
render() {
return (
<div>
<header>{this.props.widget.title}</header>
<div> /* Other widget stuff goes here */</div>
</div>
);
}
}
How can I connect each Widget component so it receives only state updates on it's own Widget object, and the connected Widget Reducer is only concerned with individual Widget objects, rather than a collection of them?