Javascript 从redux&x27;与connect helper进行本机反应;当状态更改时是否重新渲染?
我使用了Javascript 从redux&x27;与connect helper进行本机反应;当状态更改时是否重新渲染?,javascript,react-native,redux,Javascript,React Native,Redux,我使用了reactJS,我知道使用connecthelper包装的组件在其reducer的状态发生更改时侦听特定的reducer,它会导致组件重新渲染 我不知道为什么相同的过程对react native不起作用,我测试了我的动作创建者以及还原器,并百分之百地检查它们是否返回了新的状态,当我选中componentwillreceiveprops时,我发现新状态返回正确,组件不会重新渲染 减速器 const INITIAL = { isSigned: null } export defaul
reactJS
,我知道使用connect
helper包装的组件在其reducer的状态发生更改时侦听特定的reducer
,它会导致组件重新渲染
我不知道为什么相同的过程对react native
不起作用,我测试了我的动作创建者
以及还原器
,并百分之百地检查它们是否返回了新的状态
,当我选中componentwillreceiveprops
时,我发现新状态返回正确,组件不会重新渲染
减速器
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;
}
}
组件
import React, { Component } from 'react';
import { ActivityIndicator } from 'react-native';
import { connect } from 'react-redux';
import * as actions from '../../actions';
class Loading extends Component {
constructor(props){
super(props);
}
componentDidMount(){
this.props.checkSigned();
switch(this.props.isSigned){
case null : return;
case false : this.props.navigation.navigate('Auth');
case true : this.props.navigation.navigate('App')
}
}
render(){
return (
<ActivityIndicator size="large" color="black" />
)
}
}
const mapStateToProps = ({signed}) => {
const {isSigned} = signed;
return {
isSigned
}
}
export default connect(mapStateToProps, actions)(Loading);
您需要使用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});
}
}
我认为问题在于您正在componentDidMount
中运行状态更改逻辑<当您的组件重新呈现时,code>componentDidMount
不会运行,但是componentDidUpdate
会运行。把你的逻辑放在那里。不,即使我尝试过,但它不起作用,它也不是真的。bindActionCreators是分派操作的另一种方式,这意味着问题仍然是相同的更新答案@dodz事实上,我对操作和减缩器都没有问题,我提到我的减缩器工作正常,并且使用更新后的状态返回新的有效状态,但是我的组件没有重新呈现你没有state.signed,也就是说,{signed}
这就是为什么它总是undefined
并且不会重新呈现componentsigned是减速机的名称,combineReducers({signed})是真的,但是更改为componentdiddupdate
不会改变任何东西。我还尝试将console.log
放入render函数中,发现它只被调用一次,这意味着组件不会在状态更改时重新渲染
export const checkSigned = () => async dispatch => {
let token = await AsyncStorage.getItem('fb_token');
if(token){
dispatch({type: SIGNED_IN});
} else {
dispatch({type: LOGGED_OUT});
}
}