Below given code is using material-ui with reactjs. I am filtering the information of products through this sidebar function. In which there are 2 filers size and color. When we click on Expansion Panel, options will displayed and than we click on desired option (which is not returning the selected color). On the other hand same Expansion Panel is returning the correct selected "size". What i can do so that onClick={colorReducer} function return correct color. Any help will be appreciated
const Sidebar = () => {
const [size, setSize] = React.useState(0);
const [color, setColor] = React.useState("");
const sizeReducer = (e) => {
setSize(e.target.value); //This is working fine
};
const colorReducer = (e) => {
console.log(color); //It gives 0 as output, instead of selected color (type string)
setColor(e.target.value);
};
return (
<div className={classes.div}>
<ExpansionPanel>
<ExpansionPanelSummary>
<InputLabel id="label">Size</InputLabel>
</ExpansionPanelSummary>
<ExpansionPanelDetails>
<ListItem>
<MenuItem value="6" onClick={sizeReducer}>
6
</MenuItem>
<MenuItem value="7" onClick={sizeReducer}>
7
</MenuItem>
<MenuItem value="8" onClick={sizeReducer}>
8
</MenuItem>
<MenuItem value="9" onClick={sizeReducer}>
9
</MenuItem>
</ListItem>
</ExpansionPanelDetails>
</ExpansionPanel>
<ExpansionPanel>
<ExpansionPanelSummary>
<InputLabel id="label">Color</InputLabel>
</ExpansionPanelSummary>
<ExpansionPanelDetails>
<ListItem>
{setColors.map((val) => (
<MenuItem value={val} onClick={colorReducer}> //This is not returning "0", instead of color
{val}
</MenuItem>
))}
</ListItem>
</ExpansionPanelDetails>
</ExpansionPanel>
</div>
);
};
export default Sidebar;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>