我正试图通过Immutable更新我的常规React状态,并遇到了一些问题.状态不是深层嵌套的,也不是嵌套在除状态本身之外的任何东西上,例如{ "username" : "keyval" : null}}
这意味着我不能做某些事情username.update('keyval', something),而是我需要另一种方法.这是一个相当容易的问题,我只是不知道该怎么做.这是我的setState的样子,我想做一个不可变的setState动作.
handleUpdatePassword(event) {
event.persist()
this.setState(({password}) => ({
password: state.update('password', event.target.value)
})
);
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试时得到的错误:
handleUpdatePassword(event) {
event.persist()
this.setState({
password: state.update('password', event.target.value)
})
}
Run Code Online (Sandbox Code Playgroud)
此外,这有效,但我收到此错误: this.state.updater is not a function
handleUpdateUsername(event) {
console.log(this.state)
event.persist()
this.setState({
username: this.state.update('username', event.target.value)
})
}
Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用 ImmutableJS,我想使 reducer 变得不可变。我读过我需要将我的减速器转换回来才能使其正常工作。任何人都可以想出如何使这项工作的答案?本身我收到错误消息:“INCREASE_NUMBER.toObject() 不是函数”这是代码(如下):
import { INCREMENT } from '../actionTypes/default'
import { Map, toObject } from 'immutable'
const INCREASE_NUMBER = (state = Map({number: 0}), action) => {
switch(action.type) {
case INCREMENT:
return state.merge({number: state.number + 1})
default:
return state
}
};
INCREASE_NUMBER.toObject()
module.exports = INCREASE_NUMBER
Run Code Online (Sandbox Code Playgroud)