I have a parent functional component <EditCard/>
that is a modal opened when the edit table row button is selected. This edit card contains a state variable called data
which consists of the data from the table row being edited. I am setting/modifying state on <EditCard/>
with the useState
hook.
<EditCard/>
has a child component <CategoryDropdown/>
which is a dropdown that accepts a prop data.assignCategory
as its selected value and a callback handleChange()
which updates the state value data
with the value selected from the dropdown.
When I select a new value from the dropdown handleChange()
is called and setData()
is called and I see the state being updated but <CategoryDropdown/>
is not re-rendered with the new selected value.
EditCard Component Code
export default function EditCard(props) {
const [data, setData] = useState(props.data);
const handleChange = () => event => {
let d = data;
d.assignCategory = event.target.value;
setData(d);
};
let assignCategoryCol = data.assignCategory !== undefined ? <AssignCategoryCol data={data} handleChange={handleChange}/> : <></>;
return (
<div>
{assignCategoryCol}
<Button>Update</Button>
</div>
)
}
{props.data.bucketTotal}`} <Lock/></Typography>)
};
const AssignCategoryCol = (props) => {
return (
<CategoryDropdown id={props.data.id} assignedCategory={props.data.assignCategory} handleDropdownChange={props.handleChange}/>)
};
const useStyles = makeStyles(theme => ({}));
CategoryDropdown Component
class CategoryDropdown extends Component {
constructor(props) {
super(props);
//TODO Get Categories from DB and set default
this.state = {
categories: ['Select One', 'Category1', 'Category2', 'Category3'],
};
}
render() {
return (
<div id={'categoryDropdown'}>
<Select onChange={this.props.handleDropdownChange(this.props.id)} value={this.props.assignedCategory}>
{this.state.categories.map((category) => {
return <MenuItem value={category}>{category}</MenuItem>
})}
</Select>
</div>
)
}
}
const styles = theme => ({});
export default withStyles(styles)(CategoryDropdown)