I'm curious if passing setState
as a prop to a child (dumb component) is violating any "best practices" or will affect optimization.
Here is an example where I have the parent container passing state
and setState
to two child components, where the child components will call the setState
function.
I do not explicitly call setState
in the children, they reference a service to handle the correct setting of state properties.
export default function Dashboard() {
const [state, setState] = useState({
events: {},
filters: [],
allEvents: [],
historical: false,
});
return (
<Grid>
<Row>
<Col>
<EventsFilter
state={state}
setState={setState}
/>
<EventsTable
state={state}
setState={setState}
/>
</Col>
</Row>
</Grid>
)
}
Example of dashboard setState service
function actions(setState) {
const set = setState;
return function () {
return ({
setEvents: (events) => set((prev) => ({
...prev,
events,
})),
setAllEvents: (allEvents) => set((prev) => ({
...prev,
allEvents,
})),
setFilters: (name, value) => set((prev) =>
({
...prev,
filters
})
),
})
}
}
So far I haven't noticed any state issues.