I have a parent component which chooses the view to render according to a state in useReducer called state.viewOption.
The child component which dispatches it is something like:
export default function SearchFilter({ placeholder, onSearch }) {
const [state, dispatch] = useReducer(
collectionListReducer,
initialCollectionState
);
const option = state.viewOption;
const handleChange = e => dispatch(setSketchesTrendsOption(e.target.value));
return (
<Grid container>
<Grid item className={classes.selector}>
<TextField
select
id="sketches-trends-selector"
value={option}
onChange={handleChange}
>
<MenuItem value="Sketches">{t('TR_SKETCHES')}</MenuItem>
<MenuItem value="Trends">{t('TR_TRENDS')}</MenuItem>
</TextField>
</Grid>
);
}
Then the parent component where I want to choose the view option according to this state is something like:
export default function CollectionListOption() {
const [state, dispatch] = useReducer(
collectionListReducer,
initialCollectionState
);
const viewOption = state.viewOption;
useEffect(() => {
console.log('view option in useEffect', viewOption);
}, [viewOption]);
switch (viewOption) {
case 'Sketches':
return <SketchesList />;
case 'Trends':
return <TrendsList />;
default:
return <SketchesList />;
}
}
The thing is that I want to re-rerender this component <CollectionListOption/>
once state.viewOption
has changed it's value to show the right view. But I don't know why It only renders a console log when mounting and when I trigger the dispatch in <SearchFilter/>
state changes in<SearchFilter/>
but my state in<CollectionListOption/>
doesn't notice and useEffect is not triggered.
Thank you!