最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native Redux: Error Can't find variable mapStateToProps - Stack Overflow

programmeradmin2浏览0评论

I have been attempting to implement Redux into a React-Native registration app I'm working on to create a multi page form set up.

I keep getting this error:

Please review the pertaining code here under from the app's root container:

import React, { Component } from 'react';
import ReactNative from 'react-native';
import { AppRegistry,Text,View,} from 'react-native';
import { Button } from 'react-native-elements'
import { StackNavigator } from 'react-navigation'
import store from '../store/store';
import { Provider,connect } from  'react-redux';
import Register1 from './emailandpass'
import Register2   from './namefields'
//import login      from './login'
//import confirmation from './confirmation'
//import success      from './success'

class Loginscreen extends React.Component{
	static navigationOptions= {
		title: 'Wele to LearnD',
							 }
  render() {
  	const { navigate  } = this.props.navigation;
    return(
      <Provider store={store}> 
      <View>
      <Text>Have you got an account ?</Text>
      <Button
        onPress={()=> navigate('Register1')}
        title="Register here !"
        />
      </View>
      </Provider> 

    );
  }
};

const App = StackNavigator({
 	Home: { screen: Loginscreen},
 	Register1: {screen: Register1  },
 	Register2: {screen: Register2}
});

export default connect(mapStateToProps)(Landingscreen);

I have been attempting to implement Redux into a React-Native registration app I'm working on to create a multi page form set up.

I keep getting this error:

Please review the pertaining code here under from the app's root container:

import React, { Component } from 'react';
import ReactNative from 'react-native';
import { AppRegistry,Text,View,} from 'react-native';
import { Button } from 'react-native-elements'
import { StackNavigator } from 'react-navigation'
import store from '../store/store';
import { Provider,connect } from  'react-redux';
import Register1 from './emailandpass'
import Register2   from './namefields'
//import login      from './login'
//import confirmation from './confirmation'
//import success      from './success'

class Loginscreen extends React.Component{
	static navigationOptions= {
		title: 'Wele to LearnD',
							 }
  render() {
  	const { navigate  } = this.props.navigation;
    return(
      <Provider store={store}> 
      <View>
      <Text>Have you got an account ?</Text>
      <Button
        onPress={()=> navigate('Register1')}
        title="Register here !"
        />
      </View>
      </Provider> 

    );
  }
};

const App = StackNavigator({
 	Home: { screen: Loginscreen},
 	Register1: {screen: Register1  },
 	Register2: {screen: Register2}
});

export default connect(mapStateToProps)(Landingscreen);

Any help would be appreciated

Share Improve this question edited Sep 17, 2017 at 22:10 nem035 35.5k6 gold badges92 silver badges104 bronze badges asked Sep 17, 2017 at 22:07 Jake XuerebJake Xuereb 2232 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You did not create a mapStateToProps function yet you try to pass it to the connect function.
You should read the DOCS for clarity

For example:

function mapStateToProps(state) {
  return {
    navigation: state.navigation 
  }
}

This will pass the navigation from redux store as a prop to your ponent so you can access it via props.navigation

The error is pretty self-explanatory.

You're trying to pass a variable named mapStateToProps into the connect function but you never defined it.

export default connect(mapStateToProps)(Landingscreen);
//                     ^ this isn't defined anywhere

You need to actually create a function that will map the state to props:

function mapStateToProps(state) {
  // ...
}
export default connect(mapStateToProps)(Landingscreen);

You should define the function named mapStateToProps before using it:

const mapStateToProps = ( state ) => {
  const someProp = state.get('someProp');
  return {
    someProp
  }
}

发布评论

评论列表(0)

  1. 暂无评论