I am using react-redux to connect redux to my react-native project, but when I use export default connect(state => ({ count: state.count }))(Test)
method and importing to react navigation as creactStackNavigator({test:Test}) it shows
The component for route 'test' must be a React component
import MyScreen from './MyScreen';
...
test: MyScreen,
}
but when I remove connect() method it works perfectly fine
Environment:
"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>
);
}
}
Expected behavior:
It should connect to redux store and display this.props.count value
P.S : This is just demonstration of my problem ,the actual code is similar to this demo
I finally solve my problem by downgrading react-redux to 5.1.1 , the problem is that in react-redux 7.0.2 connect() method returns object but in react-navigation 2.18.2 it assumed to be function I guess, so there is a type mismatch, therefore I have to downgrade react-redux to 5.1.1 which i used earlier in different project in which connect() method return function as expected by react-navigation.
PS: Its kinda weird that I configured a new project with the above question's environment setup and it works, but not work on existing project.
I got this to work with Redux 7 and React Navigation 3 by wrapping the connected components you want to use in the Navigator like so:
import Component1 from "./Component1.js";
import Component2 from "./Component1.js";
import Component3 from "./Component1.js";
import Component4 from "./Component1.js";
const Component1Shell = () => {
return <Component1 />;
};
const Component2Shell = () => {
return <Component2 />;
};
const Component3Shell = () => {
return <Component3/>;
};
const Component4Shell = () => {
return <Component4/>;
};
const AppNavigator = createStackNavigator({
Page1: { screen: Component1Shell },
Page2: { screen: Component2Shell },
Page3: { screen: Component3Shell },
Page4: { screen: Component4Shell }
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With