【问题标题】:React + Redux Update issueReact + Redux 更新问题
【发布时间】:2017-07-18 14:31:49
【问题描述】:

我有这门课:

  import React from 'react';
  import {Link} from 'react-router';
  import '../../styles/about-page.css';
  import Item from './Item';
  // Redux
  import { connect } from 'react-redux';
  import actions from '../../redux/actions';
  import { bindActionCreators } from 'redux';
  // Auth
  import Auth from '../../modules/Auth';
  import User from '../../constants';

  class CommentForm extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        value: ''
      };
    }

    handleChange(event) {
      this.setState({value: event.target.value});
    }

    handleSubmit() {
      // alert('A name was submitted: ' + this.state.value);
      this.props.onFinish({
        name: this.props.user.name,
        userId: this.props.user.id,
        comment: this.state.value
      });
    }

    render() {
      if (!this.props.user || !this.props.user.name) return <div/>;
      return (<div className="item"> 
        {this.props.user.name}: 
        <label style={{width: '60%', margin: '10px'}}>
          <input style={{width: '100%'}} type="text" value={this.state.value} onChange={this.handleChange.bind(this)} />
        </label>
        <input style={{width: '16%', display: 'inline-block', margin: '10px'}} type="submit" value="Enviar" onClick={this.handleSubmit.bind(this)}/>
      </div>
      );
    }
  }

  @connect((state) => state)
  class ItemPage extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        user: null,
        pret: null
      };
    }

    componentWillMount() {
      let self = this;
      this.props.actions.getPret(this.props.routeParams.id).then(function(a) {
        self.setState({
          pret: self.props.pret
        })
      });
      if (Auth.isUserAuthenticated()) {
        User.getBearer(Auth, function(info) {
          self.setState({user: info});
        });
      }
    }

    onFinish(comment) {
      //changing the state in react
      //need to add '6' in the array

      //create a copy of this.state.a
      //you can use ES6's destructuring or loadash's _.clone()
      const currentStatePretCopy = Object.assign({}, this.state.pret, { b: this.state.pret.comments.concat([comment])})
      console.log(1, currentStatePretCopy);
      currentStatePretCopy.comments.push(comment);
      this.props.actions.updatePret(currentStatePretCopy);
    }

    render() {
      let self = this;
      if (!this.state || !this.state.pret) return <div/>;
      return (<div>
        <section>
          <Item full={true} user={this.state.user} item={this.state.pret} actions={this.state.actions}/>
        </section>
        <div>
          <CommentForm user={this.state.user} pret={this.state.pret} onFinish={this.onFinish.bind(this)}/>
          {/* TODO: ad here */}
          {this.state.pret.comments && this.state.pret.comments.length ? this.state.pret.comments.map(function (comment, index) {
            return (<div className="item" key={index}> by <Link to={'/user/' + comment.userId}> @{comment.name} </Link> : {comment.comment} </div>);
          }) : null}
        </div>
      </div>
      );
    }
  }

  function mapStateToProps (state) {
    return {
      pret: state.prets[0]
    };
  }

  function mapDispatchToProps (dispatch) {
    return {
      actions: bindActionCreators(actions, dispatch)
    };
  }

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

当我想更新对象时,由于 props 应该是不可变的,因此文档建议克隆对象并将其置于状态。

我正在使用新值修改状态并将其发送到 Redux 操作,但系统抱怨:

Uncaught Error: A state mutation was detected between dispatches, in the path 'prets.0.comments.1'. This may cause incorrect behavior.

由于我复制了对象,我不知道应该如何通过 React+Redux 更新商店

【问题讨论】:

    标签: reactjs redux react-redux


    【解决方案1】:

    cmets 数组仍然是原始数组。所以你正在用push 改变原始对象。 替换

    currentStatePretCopy.comments.push(comment);
    

    currentStatePretCopy.comments = currentStatePretCopy.comments.concat([comment])
    

    一切正常

    【讨论】:

    • 你成就了我的一天:D
    猜你喜欢
    • 2017-02-21
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 2021-02-25
    • 2017-08-11
    相关资源
    最近更新 更多