React passing parameter with arrow function in child component
By that feedback, I can understand what is going on to update the state.
However, it seems like my code isn't working although code seems exactly same.
constructor(props) {
super(props);
// this.state = {
// selectedId: 0
// }
this.state = {
selectedId: 0
}
}
handleClick = (id) => {
// const { name, value } = event.target;
console.log(id);
this.setState = ({
selectedId: id
})
}
render () {
//const { isEditClicked } = this.state;
const { selectedId } = this.state;
return (
<div className='admin-match-item'>
{selectedId}
<CustomIconButton type='edit' id={this.props.id} handleClick={this.handleClick} />
</div>
);
}
}
And Child component looks below
//Need refactoring
const CustomIconButton = ({ type, id, handleClick, ...otherProps }) => (
<div>
hi{id}
<button className='button-icon' onClick={() => handleClick(id)}>
{
type == 'add' ? <AddIcon className='icon' /> :
type == 'save' ? <SaveIcon className='icon'/> :
type == 'edit' ? <EditIcon className='icon' /> :
type == 'delete' ? <DeleteIcon className='icon' /> :
<ErrorIcon className='icon' />
}
</button></div>
)
export default CustomIconButton;
I am passing props.id to child component and let state to be updated with the id that I am clicking. The thing is inside of console.log is returning the value I want, but it doesn't update selectedId state(always 0) Can anyone help me with this?