Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 商店更新后未更新React redux组件道具_Javascript_Reactjs_React Native_Redux - Fatal编程技术网

Javascript 商店更新后未更新React redux组件道具

Javascript 商店更新后未更新React redux组件道具,javascript,reactjs,react-native,redux,Javascript,Reactjs,React Native,Redux,React本机组件的道具在redux存储更新后不会更新。让我感到不安的是,它以前是这样做的,然后在一个晴朗的日子,它停止了工作 分派工作正常,并且存储正确地更新状态,而不改变状态 我尝试返回一个与旧状态无关的新对象,但仍然没有运气。我也尝试过使用componentWillReceiveProps,但它不会被调用 组成部分 import React from 'react'; import { connect } from 'react-redux'; import { MonoText } fr

React本机组件的道具在redux存储更新后不会更新。让我感到不安的是,它以前是这样做的,然后在一个晴朗的日子,它停止了工作

分派工作正常,并且存储正确地更新状态,而不改变状态

我尝试返回一个与旧状态无关的新对象,但仍然没有运气。我也尝试过使用componentWillReceiveProps,但它不会被调用

组成部分

import React from 'react';
import { connect } from 'react-redux';
import { MonoText } from '../components/StyledText';
import { bindActionCreators } from 'redux';
import * as airportActions from '../../shared/redux/modules/airports';

class HomeScreen extends React.Component {
  render() {
    const { addAirports } = this.props;

    return (
      <View style={styles.container}>
        <ScrollView style={styles.container} contentContainerStyle={styles.contentContainer}>
          <View>
            <FlatList
              horizontal={true}
              data={this.props.airports.airports}
              renderItem={({item}) => <Text>{item.key + " "}</Text>}
              // extraData={this.state}
            />
          </View>
          <Button
            onPress={() => this.props.switchAirports()}
            title="SWITCH"
            color="#841584"
            accessibilityLabel="Learn more about this purple button"
          />
        </ScrollView> 
      </View>
    );
  }

const mapStateToProps = (state) => {
  const { airports } = state
  return { airports }
};

const mapDispatchToProps = dispatch => (
  bindActionCreators(airportActions, dispatch)
);

export default connect(mapStateToProps, mapDispatchToProps)(HomeScreen);

组合减速器

import { combineReducers } from 'redux';
import airports from './airports';
import galleryState from './gallery'

export default combineReducers({
    galleryState,
  airports,
});
当我点击切换按钮时,我希望前两个机场可以切换。它们在商店中是这样的,我再次单击“切换”后,它们将在商店中切换。但是,这些状态更改从未反映在组件的支柱上


同样,这曾经奏效,但它突然停止了工作。不仅是这个部件,还有另一个和另一个减速器。

我知道这已经快两年了,但是

在减速机代码中:
Object.assign()
仅执行浅复制。因此,您的
newState.airports
数组仍然指向
state.airports
数组

这意味着
[newState.airports[0],newState.airports[1]=[newState.airports[1],newState.airports[0]]正在改变状态

假设您的
状态。airports
数组的深度为1,并且您的状态没有进一步嵌套,请尝试:

...
  const newState = {...state, airports: [...state.airports]};
...

您可以阅读更多。

不必依赖
这个.props.switchAirports()
,您可以执行
这个.props.dispatch({type:SWITCH})
?您的
App.js中是否有
Provider
标记?
提供程序
使存储的所有子组件都可以访问存储。
Provider
标记是任何React with Redux应用程序的绝对根组件,甚至是React Native。感谢您的响应。我在
App.js
中有
Provider
标签。另外,当我尝试执行
this.props.dispatch({type:SWITCH})
时,我得到了一个错误,因为
this.props.dispatch
没有定义。我遇到了同样的问题。你找到解决办法了吗?
...
  const newState = {...state, airports: [...state.airports]};
...