I have a table with 2 radio buttons in each row to either approve or reject the row. The onChange event saves the ID of each row and if it was approved or rejected. There is then a button to save this in the back end.
That all works fine.
import React from 'react';
import { Button } from 'reactstrap';
class ApproveDataList extends React.Component {
constructor(props) {
super(props);
this.state = {
changedValues : {}
};
}
onChange = (selectedIndex, e) => {
var changedValues = Object.assign({}, this.state.changedValues);
changedValues[selectedIndex] = e.target.value;
this.setState({ changedValues });
}
save(e) {
//save this.state.changedValues
}
render() {
return (
<div>
<table>
<thead>
<tr>
<th>Id</th>
<th>Approve</th>
<th>Reject</th>
</tr>
</thead>
<tbody>
{this.props.approveData.map((approveData) =>
<tr >
<td>{approveData.signoffId}</td>
<td>
<div class="radio">
<input
type="radio"
value={"approve" + approveData.signoffId}
name={"radio" + approveData.signoffId}
onChange={this.onChange.bind(this, approveData.signoffId)} />
</div>
</td>
<td>
<div class="radio">
<input
type="radio"
value={"reject" + approveData.signoffId}
name={"radio" + approveData.signoffId}
onChange={this.onChange.bind(this, approveData.signoffId)} />
</div>
</td>
</tr>
)}
</tbody>
</table>
<Button onClick={this.save.bind(this)} >Save</Button>
</div>
);
}
}
export default ApproveDataList
I'm now confused on how to set the initial state of the radio buttons when I load this component from the back end.
My gut feeling was to do
... value={"approve" + approveData.signoffId} checked={approveData.signedOff}
and
... value={"reject" + approveData.signoffId} checked={!approveData.signedOff}
But of course this won't work. I think I need to store the initial values in State and use that but not sure how to do this as I'm new to React and haven't found a specific solution online.
Please help!