samedi 29 juillet 2017

How to pass state from checkboxes to a parent component?

I have a class (Cookbook) that passes a click handler "handleOnChange" to a child component (Filter). Filter renders checkboxes. On those checkboxes, I execute the handleOnChange. I want to update the state that exists in Cookbook, but even though handleOnChange is declared in Cookbook, it does not recognize this.state.selectedStaples. The error takes place in the handleOnChange function on the first non-console.log line. How do I update the state of selectedStaples in Cookbook?

Here is Cookbook

class Cookbook extends React.Component {
    constructor(){
        super(); 
        this.state ={
            cookbook: data,
            selectedStaples: [],
        }   
    }

    getStapleList(recipes){
        let stapleList = [];
        recipes.forEach(function(recipe){
            recipe.ingredient_list.forEach(function(list){
                stapleList.push(list.needed_staple);
            });
        });

        let flattenedStapleList = stapleList.reduce(function(a,b){
            return a.concat(b);
        })

        return flattenedStapleList
    }

    handleOnChange(staple, isChecked, selectedStaples) {
        console.log(staple);
        console.log(isChecked);
        console.log(selectedStaples);
        const selectedStaples = this.state.selectedStaples;
        // if (isChecked) {
        //     this.setState({
        //         selectedStaples: selectedStaples.push(staple)
        //     })
        // } else {
        //     this.setState({
        //         selectedStaples: selectedStaples.filter(selectedStaple => selectedStaple !== staple)
        //     })
        // }
    }

    render(){
        const selectedStaples = this.state.selectedState;
        const cookbook = this.state.cookbook;
        const stapleList = this.getStapleList(cookbook.recipes);
        return(
            <div className="cookbook">
                <div className="filter-section">
                    <h1>This is the filter section</h1>
                    <Filter stapleList = {stapleList} onChange={this.handleOnChange}/>
                </div>
                <div className="recipes">
                    <h1>This is where the recipes go</h1>
                    <div className="recipe">    
                        <Recipe cookbook = {cookbook}/>
                    </div>

                </div>
            </div>
        );
    }
}

and Filter

class Filter extends React.Component {


    render(){
        const stapleList = this.props.stapleList;
        const checkboxItems = stapleList.map((staple, index) => 
            <div key = {index}>
                <label>
                    <input
                        type="checkbox"
                        value="{staple}"
                        onClick={e => this.props.onChange(staple, e.target.checked)}
                    />
                    {staple}
                </label>
            </div>
        );
        return (
            <form>
                {checkboxItems}
            </form>
        );
    }
}




Aucun commentaire:

Enregistrer un commentaire