动作分派后 mapStateToProps 未反映在组件中
mapStateToProps is not reflecting in component after action dispatch
我有一个看起来像这样的减速器:
export default function reducer(state={
fetching: false,
}, action) {
switch (action.type) {
case 'LOGIN_REQUEST': {
return {...state, fetching: true}
}
...
return state
我从组件向它发送操作,在我有的组件中:
const mapStateToProps = (state) => {
return state
}
我的问题是:
1.在mapStateToProps
状态包含login.fetching: true
2. 在向 reducer this.props.login.fetching
发送 action 之后的组件中包含 false
.
3. 在组件 render()
中,方法 this.props.login.fetching
是 true
。
为什么万一 2. 它仍然是 false
,这可能吗?我在这里缺少什么?
动作调度:
onLoginPress = () => {
this.props.dispatch(loginUser(this.state.email.trim(), this.state.password.trim()))
console.log(this.props);
答案很简单,在 dispatch
一个动作之后你会得到 false
因为 Javascript 的 async nature
,所以甚至在你 [=14] 之前=] 得到修改你的 console.log(this.props)
被调用,因此它显示以前的值。但是,当您从 reducer 更改状态时,将调用 render()
函数,因此它会显示更新后的值。
你需要的是 Promise
派送
使用这个
onLoginPress = () => {
this.props.dispatch(loginUser(this.state.email.trim(), this.state.password.trim())).then(() => {
console.log(this.props);
})
我希望,我能够正确解释它。
我有一个看起来像这样的减速器:
export default function reducer(state={
fetching: false,
}, action) {
switch (action.type) {
case 'LOGIN_REQUEST': {
return {...state, fetching: true}
}
...
return state
我从组件向它发送操作,在我有的组件中:
const mapStateToProps = (state) => {
return state
}
我的问题是:
1.在mapStateToProps
状态包含login.fetching: true
2. 在向 reducer this.props.login.fetching
发送 action 之后的组件中包含 false
.
3. 在组件 render()
中,方法 this.props.login.fetching
是 true
。
为什么万一 2. 它仍然是 false
,这可能吗?我在这里缺少什么?
动作调度:
onLoginPress = () => {
this.props.dispatch(loginUser(this.state.email.trim(), this.state.password.trim()))
console.log(this.props);
答案很简单,在 dispatch
一个动作之后你会得到 false
因为 Javascript 的 async nature
,所以甚至在你 [=14] 之前=] 得到修改你的 console.log(this.props)
被调用,因此它显示以前的值。但是,当您从 reducer 更改状态时,将调用 render()
函数,因此它会显示更新后的值。
你需要的是 Promise
派送
使用这个
onLoginPress = () => {
this.props.dispatch(loginUser(this.state.email.trim(), this.state.password.trim())).then(() => {
console.log(this.props);
})
我希望,我能够正确解释它。