Reactjs 反应+;Redux更新问题
我有这门课:Reactjs 反应+;Redux更新问题,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我有这门课: 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 {
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);
从“React”导入React;
从“反应路由器”导入{Link};
导入“../styles/about page.css”;
从“./Item”导入项目;
//重演
从'react redux'导入{connect};
从“../../redux/actions”导入操作;
从“redux”导入{bindActionCreators};
//认证
从“../../modules/Auth”导入验证;
从“../../constants”导入用户;
类CommentForm扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
值:“”
};
}
手变(活动){
this.setState({value:event.target.value});
}
handleSubmit(){
//警报(“已提交名称:”+this.state.value);
this.props.onFinish({
名称:this.props.user.name,
userId:this.props.user.id,
注释:this.state.value
});
}
render(){
如果(!this.props.user | |!this.props.user.name)返回;
报税表(
{this.props.user.name}:
);
}
}
@连接((状态)=>状态)
类ItemPage扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
用户:null,
pret:null
};
}
组件willmount(){
让自我=这个;
this.props.actions.getPret(this.props.routeParams.id){
自我状态({
pret:self.props.pret
})
});
if(Auth.isUserAuthenticated()){
User.getBearer(身份验证,函数(信息){
self.setState({user:info});
});
}
}
onFinish(注释){
//在react中更改状态
//需要在数组中添加“6”
//创建此.state.a的副本
//您可以使用ES6的destructuring或loadash的z.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.updateSet(currentStatePretCopy);
}
render(){
让自我=这个;
如果(!this.state | |!this.state.pret)返回;
返回(
{/*TODO:ad此处*/}
{this.state.pret.comments&&this.state.pret.comments.length?this.state.pret.comments.map(函数(注释,索引){
返回(按{comment.name}:{comment.comment});
}):null}
);
}
}
函数MapStateTops(状态){
返回{
pret:state.prets[0]
};
}
功能图DispatchToprops(调度){
返回{
操作:bindActionCreators(操作、调度)
};
}
导出默认连接(
MapStateTops,
mapDispatchToProps
)(第页);
当我想要更新对象时,由于props应该是不可变的,文档建议克隆对象并将其置于状态
我正在使用新值修改状态,并将其发送给Redux操作,但系统抱怨:
Uncaught错误:在路径“prets.0.comments.1”的调度之间检测到状态突变。这可能会导致不正确的行为。
由于复制了对象,我不知道如何通过React+Redux更新存储注释数组仍然是原始数组。因此,您正在使用
push
对原始对象进行变异。
替换
与
一切都会好起来的你让我开心了:D
currentStatePretCopy.comments.push(comment);
currentStatePretCopy.comments = currentStatePretCopy.comments.concat([comment])