当状态改变时,redux的带有连接助手的本机反应不会重新呈现吗?

时间:2018-08-17 22:58:45

标签: javascript react-native redux

我使用了reactJS,我知道一个包裹着connect帮助器的组件会在其reducer发生变化时侦听特定的reducer's state,从而导致该组件{ 1}}。

我不知道为什么相同的过程对re-render不起作用,我测试了react-nativeaction creators并检查了百分百它们返回新的{{1} },并且当我检查reducers时,我发现正确返回了新状态并且该组件未重新呈现。


减速器

state

组件

componentWillRecieveProps

操作

const INITIAL = {
  isSigned: null
}

export default (state = INITIAL, action) => {
  switch(action.type){
    case SIGNED_IN : return {...state, isSigned: true};
    case LOGGED_OUT: return {...state, isSigned: false};
    default: return state;
  }
}

2 个答案:

答案 0 :(得分:0)

您需要使用bindActionCreators来将您的操作作为道具发送

import { bindActionCreators } from 'redux';


const mapDispatchToProps = dispatch => bindActionCreators(actions, dispatch);
const mapStateToProps = state => {
  return {
    isSigned: state.isSigned
  }
}

export default connect(mapStateToProps, actions)(Loading);

//在操作中,您需要修复操作代码

export const checkSigned = () => async dispatch => {
let token = await AsyncStorage.getItem('fb_token');
 if(token){
   dispatch({type: SIGNED_IN});
 } else {
    dispatch({type: LOGGED_OUT});
 }
}

答案 1 :(得分:0)

我认为问题在于您正在componentDidMount中运行状态更改逻辑。重新发布组件时componentDidMount不会运行,但是componentDidUpdate会运行。把你的逻辑放在那里。

相关问题