简体   繁体   English

React Native Redux:错误无法找到变量mapStateToProps

[英]React Native Redux: Error Can't find variable mapStateToProps

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. 我一直在尝试将Redux实现到React-Native注册应用程序中,我正在努力创建一个多页面表单设置。

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: 'Welcome 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 任何帮助,将不胜感激

You did not create a mapStateToProps function yet you try to pass it to the connect function. 您没有创建mapStateToProps函数,但是您尝试将其传递给connect函数。
You should read the DOCS for clarity 为清楚起见,您应该阅读DOCS

For example: 例如:

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

This will pass the navigation from redux store as a prop to your component so you can access it via props.navigation 这会将redux storenavigation作为道具传递给您的组件,以便您可以通过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. 您正在尝试将名为mapStateToProps的变量传递给connect函数,但您从未定义过它。

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

You need to actually create a function that will map the state to props: 您需要实际创建一个将状态映射到props的函数:

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

You should define the function named mapStateToProps before using it: 在使用它之前,您应该定义名为mapStateToProps的函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM