我有这个变量
const routes = [{data : 'Home' , icon : 'home' , keep:true , txt:'Home'} , {data : 'NECard' , icon : 'card' , keep:true , txt:'Your Card'} , {data : 'Notifications' , icon : 'notifications' , color : '#EB4D4B' , count : 27 , keep:true , txt:'Notifications'} , {data : 'Requests' , icon : 'contacts' , color : '#0097E6' , count : 8 , keep:true , txt:'Requests'} , {data : 'Saved' , icon : 'bookmark' , color : '#FBC531' , count : 51 , keep:true , txt:'Saved'} , {data : 'Logout' , icon : 'ios-exit' , keep:true , txt:'Logout'}];
我在构造函数中设置状态
this.state = {routes : routes};
在安装组件之后,我也运行了
componentDidMount()
{
this.fetchNotReadedNotis();
}
最后一步
async fetchNotReadedNotis()
{
this.state.routes[2].count = 392;
this.forceUpdate();
}
使用setState也不起作用
async fetchNotReadedNotis()
{
var rts = this.state.routes;
rts[2].count = 392;
this.setState({routes:rts});
}
期望将route [2] count属性设置为392
但没有任何变化,仍然是27
我的代码有问题吗?
答案 0 :(得分:2)
您应该使用this.setState()
函数来更新反应状态。您不应该直接改变状态。阅读文档here
答案 1 :(得分:0)
您不应直接在React中对state
进行突变。您应该使用setState
来更新状态。
示例
class App extends React.Component {
state = {
routes: [
{ data: "Home", icon: "home", keep: true, txt: "Home" },
{ data: "NECard", icon: "card", keep: true, txt: "Your Card" },
{
data: "Notifications",
icon: "notifications",
color: "#EB4D4B",
count: 27,
keep: true,
txt: "Notifications"
},
{
data: "Requests",
icon: "contacts",
color: "#0097E6",
count: 8,
keep: true,
txt: "Requests"
},
{
data: "Saved",
icon: "bookmark",
color: "#FBC531",
count: 51,
keep: true,
txt: "Saved"
},
{ data: "Logout", icon: "ios-exit", keep: true, txt: "Logout" }
]
};
componentDidMount() {
this.fetchNotReadedNotis();
}
fetchNotReadedNotis = async () => {
this.setState(previousState => {
const routes = [...previousState.routes];
return {
routes: [
...routes.slice(0, 2),
{ ...routes[2], count: 392 },
...routes.slice(3)
]
};
});
};
render() {
return (
<div>
{this.state.routes.map(route => (
<div key={route.data}>
{route.data} {route.count}
</div>
))}
</div>
);
}
}
答案 2 :(得分:0)
您应该直接更新状态。执行以下操作:
fetchNotReadedNotis = async () => {
var routes = Object.assign([],this.state.routes)
routes[2].count = 392
this.setState({routes: routes},() => { this.forceUpdate() })
}
详细信息:您的fetchNotReadedNotis
未绑定到组件,因此无法更改其状态。方法中的this
引用不是您的组件。现在,执行fetchNotReadedNotis = async () => {}
,它将绑定到您的组件,然后它将正确地改变状态。
class App extends React.Component {
constructor(props){
super(props)
this.state = {
routes: [{data : 'Home' , icon : 'home' , keep:true , txt:'Home'} , {data : 'NECard' , icon : 'card' , keep:true , txt:'Your Card'} , {data : 'Notifications' , icon : 'notifications' , color : '#EB4D4B' , count : 27 , keep:true , txt:'Notifications'} , {data : 'Requests' , icon : 'contacts' , color : '#0097E6' , count : 8 , keep:true , txt:'Requests'} , {data : 'Saved' , icon : 'bookmark' , color : '#FBC531' , count : 51 , keep:true , txt:'Saved'} , {data : 'Logout' , icon : 'ios-exit' , keep:true , txt:'Logout'}]
}
}
componentDidMount(){
this.fetchNotReadedNotis();
}
fetchNotReadedNotis = () => {
var routes = Object.assign([],this.state.routes)
routes[2].count = 392
this.setState({routes: routes},() => { this.forceUpdate()})
}
render() {
return(
<div>
{this.state.routes.map(route => <p> {route.data} {route.count}</p>)}
</div>
)
}
}
ReactDOM.render(<App/>,document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"> </div>