【问题标题】:Redux React-Bootstrap Collapsible Panel issueRedux React-Bootstrap 可折叠面板问题
【发布时间】:2017-01-06 05:35:36
【问题描述】:

所以我基本上完成了 FCC 的RecipeBox Project 之一

我唯一遇到的问题是跟踪面板的折叠状态。

换句话说,每次我更改应用程序的状态(无论是添加、删除还是编辑配方)时,它都会重新呈现页面,并且每个面板都会将其展开。

我查看了React-Bootstrap Document,但我无法弄清楚。

在为每个配方项应用状态时,我当前的代码不起作用

src/containers/recipebox

class RecipeBox extends Component {
  constructor(props){
    super(props);
    this.state = {open: false}
    this.renderRecipeList = this.renderRecipeList.bind(this)
    this.handleRecipeNameChange = this.handleRecipeNameChange.bind(this)
    this.handleUserIngredientsChange = this.handleUserIngredientsChange.bind(this)
  }

  handleRecipeNameChange(event){
    this.setState({recipeName: event.target.value})
  }
  handleUserIngredientsChange(event){
    this.setState({userIngredients: event.target.value})
  }
  renderRecipeList(recipeItem, index){
    const recipe = recipeItem.recipe;
    const ingredients = recipeItem.ingredients;
    const recipeID = recipeItem.id;
    const id = shortid.generate();
    return(
      <div key={id}>
        <Panel
          collapsible
          onClick={ ()=> this.setState({ open: !this.state.open })}
          eventKey={index}
          header={<h3>{recipe}</h3>}>
          <ListGroup >
            <ListGroupItem  header="Ingredients"></ListGroupItem>
            {ingredients.map(function(ingredient,index){
              return <ListGroupItem key={index}>{ingredient}</ListGroupItem>;
            })}
            <ListGroupItem>
              <Button
                onClick={() => this.props.deleteRecipe(recipeItem)}
                bsStyle="danger">Delete
              </Button>
              <Edit
                recipeName={recipe}
                userIngredients={ingredients}
                recipeID={recipeID}
              />
            </ListGroupItem>
          </ListGroup>
        </Panel>
      </div>
    )
  }
  render(){
    const self = this;
    return(
      <div className="container">
        <div className='panel-group'>
          {this.props.recipeList.map(self.renderRecipeList)}
        </div>
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {
    recipeList : state.recipeState
  };
}

function mapDispatchToProps(dispatch){
  return bindActionCreators({deleteRecipe : deleteRecipe, editRecipe : editRecipe}, dispatch)
}

export default connect(mapStateToProps,mapDispatchToProps)(RecipeBox);

【问题讨论】:

    标签: twitter-bootstrap reactjs redux react-redux collapsable


    【解决方案1】:

    所以我能够找出问题所在:我需要每个配方都有自己的组件状态。

    我创建了一个单独的组件,RecipeList,它呈现每个食谱项,并拥有自己的状态组件。

    我是这样修改的:

    src/containers/recipebox

        import React, { Component } from 'react';
        import { connect } from 'react-redux';
        import { ListGroup, ListGroupItem, Panel, Button, Modals } from 'react-bootstrap';
        import RecipeList from '../containers/recipe_list'
    
        class RecipeBox extends Component {
          constructor(props){
            super(props);
          }
          render(){
            let itemList = [];
            this.props.recipeList.forEach(function(item){
              itemList.push(
                <RecipeList
                  key={item.id}
                  recipeID={item.id}
                  recipe={item.recipe}
                  ingredients={item.ingredients}
                />)
            })
            return(
              <div className="container">
                <div className='panel-group'>
                  {itemList}
                </div>
              </div>
            )
          }
        }
    
        function mapStateToProps(state) {
          return {
            recipeList : state.recipeState
          };
        }
    
        export default connect(mapStateToProps)(RecipeBox);
    

    src/containers/recipe_list

    import React, { Component } from 'react';
    import { connect } from 'react-redux';
    import { ListGroup, ListGroupItem, Panel, Button, Modals } from 'react-bootstrap';
    import { bindActionCreators } from 'redux';
    import { deleteRecipe, editRecipe } from '../actions/index';
    import shortid from 'shortid'
    import Edit from '../containers/edit_recipe'
    
    class RecipeList extends Component {
      constructor(props){
        super(props);
        this.state = {open: false}
      }
      handleRecipeNameChange(event){
        this.setState({recipeName: event.target.value})
      }
      handleUserIngredientsChange(event){
        this.setState({userIngredients: event.target.value})
      }
      render(){
        return(
          <div>
            <Panel
              collapsible
              header={<h3>{this.props.recipe}</h3>}>
              <ListGroup >
                <ListGroupItem  header="Ingredients"></ListGroupItem>
                {this.props.ingredients.map(function(ingredient,index){
                  return <ListGroupItem key={index}>{ingredient}</ListGroupItem>;
                })}
                <ListGroupItem>
                  <Button
                    onClick={() => this.props.deleteRecipe(this.props.recipeID)}
                    bsStyle="danger">Delete
                  </Button>
                  <Edit
                    recipeName={this.props.recipe}
                    userIngredients={this.props.ingredients}
                    recipeID={this.props.recipeID}
                  />
                </ListGroupItem>
              </ListGroup>
            </Panel>
          </div>
        )
      }
    }
    
    function mapDispatchToProps(dispatch){
      return bindActionCreators({deleteRecipe, editRecipe}, dispatch)
    }
    
    export default connect(null,mapDispatchToProps)(RecipeList);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 2016-02-16
      • 1970-01-01
      相关资源
      最近更新 更多