0

当我尝试插入新的配方元素时,我的项目不断崩溃。我使用this.state.recipes.map...inRecipeList能够根据需要更新食谱(例如删除、编辑等)。删除功能有效,但我无法添加新的配方元素。
如果我将语句切换为this.props.recipes.map...,我可以毫无问题地插入元素,但由于删除会触发状态更改,因此无法删除,并且需要状态更改来反映更新而不是道具。有人对这个问题有任何提示吗?谢谢!

食谱清单:

var RecipeList = React.createClass({
getInitialState: function(){
    return {recipes: []};
},
deleteRecipe: function(recipe){
    var curRecipes = this.state.recipes.slice('');
    curRecipes.splice(recipe.recipeKey,1);
    this.setState({recipes: curRecipes});
},
componentWillMount: function(){
    this.setState({recipes: this.props.recipes});
},
render: function(){

    var recipeNodes = this.state.recipes.map(function(recipe,index){
        return <Recipe onDelete={this.deleteRecipe} recipeKey={index} key={index} recipeTitle={recipe.recipeTitle} ingredients={recipe.ingredients}  instructions={recipe.instructions} />
    },this);
    return(
        <div>
            {recipeNodes}
        </div>
    );
}
});

配方容器:

var RecipeBox = React.createClass({
getInitialState: function(){
    return {showForm: false,
            recipes: []
        };
},
openForm: function(){
    this.setState({showForm: true});
},
handleRecipeSubmit: function(recipe){
    var curRecipes = this.state.recipes.slice('');
    curRecipes.push({recipeTitle: recipe.recipeTitle,ingredients: recipe.ingredients, instructions: recipe.instructions});
    this.setState({recipes: curRecipes});
},
render: function(){
    return(
        <div id="recipeBox">
            <RecipeList recipes={this.state.recipes} />
            <div className="recipeButtons">
                <button id="addRecipeButton" className="btn-style" onClick={this.openForm}>Add Recipe</button>
            </div>
            {this.state.showForm ? this.refs.dialogWithCallBacks.show() : null}
            <SkyLight
                dialogStyles={formDialog}
                ref="dialogWithCallBacks"
                title="Add Recipe">
                <RecipeForm onRecipeSubmit={this.handleRecipeSubmit} skylightRef={this.refs.dialogWithCallBacks} />
            </SkyLight>
        </div>
    );
}
});

配方表:

var RecipeForm = React.createClass({
getInitialState: function(){
    return {hideDialog: false};
},
getFormData: function(){
    var ingredients= document.getElementsByClassName("ingredient"),
        recipeName = document.getElementsByName('recipeName')[0].value,
        instructions = document.querySelector('textarea').value,
        data = [];

    ingredients = [].slice.call(ingredients).map(function(ingredient,index){
        return {
            "quantity": ingredient.childNodes[0].value,
            "ingredient": ingredient.childNodes[1].value,
            "unit": ingredient.childNodes[2].value
        };
    });

    // Combine results into output array
    data.push(recipeName);
    data.push(ingredients);
    data.push(instructions);

    return data;
},
submitRecipe: function(event){
    event.preventDefault();
    var data = this.getFormData();

    // Hide the SkyLight modal container
    this.setState({hideDialog: true});

    // Submit form
    this.props.onRecipeSubmit({recipeTitle: data[0], ingredients: data[1], instructions: data[2]});
},
render: function(){

    return(
        <form onSubmit={this.submitRecipe}>
            <section className="recipe-main">
                <h2 style={{'border-bottom': 'none'}}>Recipe Name</h2>
                <RecipeFormName />
                <h2 style={{'border-bottom': 'none'}}>Ingredients</h2>
                <RecipeFormIngredients />
            </section>
            <RecipeFormInstructions />
            <input type="submit" value="Add Recipe" />
            {this.state.hideDialog ? this.props.skylightRef.hide() : null}
        </form>
    )
}
});
4

2 回答 2

1

需要将 RecipeList 组件更改为

<RecipeList recipes={this.state.recipes} onChange={this.handleChange}/>

然后从 中RecipeBox而不是直接在 中处理删除更改RecipeList。必须用于this.props.map...显示新食谱并删除可见的食谱。

var RecipeList = React.createClass({
    getInitialState: function(){
        return {recipes: this.props.recipes};
    },
    deleteRecipe: function(recipe){
        var curRecipes = this.props.recipes.slice('');
        curRecipes.splice(recipe.recipeKey,1);
        this.props.onChange({recipes: curRecipes});
    },
    render: function(){

        var recipeNodes = this.props.recipes.map(function(recipe,index){
            return <Recipe onDelete={this.deleteRecipe} recipeKey={index} key={index} recipeName={recipe.recipeName} ingredients={recipe.ingredients}      instructions={recipe.instructions} />
        },this);

        return(
            <div>
                {recipeNodes}
            </div>
        );
    }
});
于 2016-08-08T01:26:09.137 回答
1

您应该将 componentWillMount 中的代码移动到 getInitialState。

getInitialState: function(){
    return {recipes: this.props.recipes};
},
于 2016-08-07T05:46:24.463 回答