在react-navigation中使用react-redux connect方法显示错误“路由'x'的组件必须是React组件

问题描述 投票:0回答:2

我正在使用react-redux将redux连接到我的react-native项目,但当我使用export default connect(state => ({ count: state.count }))(Test) 方法并导入以反映导航为creactStackNavigator({test:Test})时,它显示

The component for route 'test' must be a React component

import MyScreen from './MyScreen';
...
test: MyScreen,
}

但是,当我删除connect()方法时,它的工作完全正常

环境:

"react":16.8.4
"react-native":0.59.4
"react-redux": "7.0.2",
"redux": "4.0.1",
"react-navigation": "2.18.2",

Test.js

import React, { Component } from 'react';
import { Text, View,Button} from 'react-native';
import  {connect}  from 'react-redux';

class Test extends Component {

  render() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>count:{this.props.count}</Text>
         <Button title="Increment" onPress={() => this.props.incrementCounter()}></Button>
         <Button title="Decrement" onPress={() => this.props.decrementCounter()}></Button>
      </View>
    )
}
}
const mapDispatchToProps = dispatch =>({
  incrementCounter: _ => dispatch({type:"INCREMENT_COUNTER"}),
  incrementCounter: _ => dispatch({type:"DECREMENT_COUNTER"}),
})
export default connect(state => ({ count: state.count }),mapDispatchToProps )(Test)

reducers.js


const INITIAL_STATE = {
    count: 1
}
export default (state = INITIAL_STATE, action) => {
    switch (action.type) {
        case 'COUNTER_INCREMENT': {
            return (Object.assign({}, { ...state, count: state.count + 1}))
        }
        case 'COUNTER_DECREMENT': {
            return Object.assign({},{ ...state,  count: state.count - 1 });
        }
        default: {
            return state;
        }
    }
}

App.js

import React, { Component } from 'react';
import { Platform, } from 'react-native';
import { Provider } from 'react-redux';

import { createStackNavigator} from 'react-navigation';
import Test from './src/screens/test';
import Screen2 from './src/screens/screen2';
import { createStore } from 'redux';
import reducers from './reducers';

const store = createStore(reducers);

const AppNavigator = createStackNavigator({
  test: Test,
  screen2: Screen2
}, {
    headerMode: "none"
  });

export default class App extends Component {

  render() {
    return (
      <Provider store={store}>
        <AppNavigator />
      </Provider>
    );
  }
}

预期行为:

它应该连接到redux存储并显示this.props.count

P.S:这只是我的问题的演示,实际代码与此演示类似

react-native redux react-redux react-navigation
2个回答
2
投票

我最终通过将react-redux降级到5.1.1来解决我的问题,问题是在react-redux 7.0.2中connect()方法返回对象但是在react-navigation 2.18.2它假设是函数我猜,所以有类型不匹配,因此我我必须将react-redux降级到5.1.1,这是我之前在不同项目中使用的,其中connect()方法按照反应导航的预期返回功能。

PS:我用上述问题的环境设置配置了一个新项目并且它有效但有点奇怪,但不适用于现有项目。


-1
投票

您必须在组件中使用mapStateToProps函数,如下所示

function mapStateToProps(state) {
    return {
        state
    }
}

function mapDispatchToProps() {
... your code to dispatch actions...
}

export default connect(mapStateToProps, mapDispatchToProps)(yourComponentName)
© www.soinside.com 2019 - 2024. All rights reserved.