reactjs - React Checkbox Group - Set initial state from API -
i have checkboxgroup
component takes in options
array prop , generates checkboxinput
components. on page load make call api returns array of pre-selected checkboxes (delivered value
prop). depending on logged in user, call can return empty array or selection of selected checkbox options.
the following code takes response of api call , sets relevant checkboxes 'checked'. issue have code doesn't allow me make changes checkboxes after page load (clicking checkboxes has no effect).
i think there disconnect between initial selectedcheckboxes state , value of api call read setting props initial state anti-pattern (e.g. selectedcheckboxes: props.value,
).
export default class checkboxgroup extends component { constructor(props) { super(props); this.state = { selectedcheckboxes: [], }; } addcheckboxtoselected = (id) => { if (this.state.selectedcheckboxes.includes(id)) { // remove checkbox array , update state const filteredarray = this.state.selectedcheckboxes.filter(item => item !== id); this.setstate({ selectedcheckboxes: filteredarray }); } else { // add checkbox array , update state this.setstate({ selectedcheckboxes: this.state.selectedcheckboxes.concat(id) }); } } checkifselected = (checkboxvalue) => { const preselectedcheckboxes = this.props.value; let selectedbool = false; preselectedcheckboxes.some(function(object) { if (object.id === checkboxvalue) { selectedbool = true; } return false; }); return selectedbool; } render() { const { label, name, options } = this.props; return ( <div classname="form-group form-inline"> <span classname="checkboxgroup-heading">{label}</span> <div classname="form-group-container"> {options.map(object => ( <checkboxinput key={object.value} name={name} label={object.label} onchange={this.addcheckboxtoselected} value={object.value} checked={this.checkifselected(object.value)} /> ))} </div> </div> ); } }
this stateless checkboxinput component
const checkboxinput = ({ name, label, onchange, value, checked }) => { return ( <div classname="field form-group filter-input"> <input type="checkbox" id={value} name={name} value={value} onchange={() => onchange(value)} checked={checked} /> <label htmlfor={value} classname="form-control">{label}</label> </div> ); };
check following code snippet. might help. let me know if have questions.
const checkboxfield = ({checked, onchange}) => { return ( <input type="checkbox" checked={checked} onchange={ev => onchange(ev.target.checked)} /> ); }; class app extends react.component { constructor() { super(); this.state = { options: [{id: "1", checked: true}, {id: "2", checked: false}] }; } handlecheckboxchange(checked, option) { const {options} = this.state; var coptions = [...options]; for(var in coptions) { if(coptions[i].id == option.id) { coptions[i].checked = checked; } } this.setstate({ options: coptions }, () => console.log(options)); } render() { const {options} = this.state; return ( <div> { options.map(option => { return ( <checkboxfield key={option.id} checked={option.checked} onchange={value => this.handlecheckboxchange(value, option)} /> ) }) } </div> ); } } reactdom.render(<app />, document.getelementbyid("root"));
<div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
Comments
Post a Comment