【问题标题】:Prop state is being. indentified as "undefined"道具状态正在。标识为“未定义”
【发布时间】:2019-08-18 02:50:03
【问题描述】:

我是 Redux 的新手,并试图在我的应用程序中设置一个简单的身份验证系统。我正在尝试检查全局存储中是否存在令牌并相应地重定向到新窗口。但是,身份验证状态是未定义的。我使用过 React Native、React Navigation 和 Redux。

我得到的错误:
Screenshot of error - iOS

代码 SingupScreen.js

class SignupScreen extends React.Component{

    constructor(props){        
        super(props);        
        this.state = {
            firstName: '',
            lastName: '',
            email: '',
            password: '',
            mobile: '',
            reCheck: false,
            flag: false   
        }

    }    

    static navigationOptions = {
        title: "Signup for NDRT 24"
    }        

    componentDidMount(){
        if (this.props.auth.token != "" || this.props.auth.token != null) this.props.navigation.navigate("LoginScreen")
    }

    render(){
        return(
            <Container style={styles.signupScreen}>                
                <Content padder>                    
                    <Form>
                        <Row>
                            <Col>
                                <Item floatingLabel>
                                    <Label>First Name</Label>
                                    <Input autoCorrect={false} autoCapitalize="none" onChangeText={(text)=>this.handleFirstName(text)} />
                                </Item>
                            </Col>
                            <Col>
                                <Item floatingLabel>
                                    <Label>Last Name</Label>
                                    <Input autoCorrect={false} autoCapitalize="none" onChangeText={(text)=>this.handleLastName(text)} />
                                </Item>
                            </Col>
                        </Row>
                        <Item floatingLabel>
                            <Label>Enter Password</Label>
                            <Input secureTextEntry autoCorrect={false} autoCapitalize="none" onChangeText={(text)=>this.handlePassword(text)} />
                        </Item>
                        <Item floatingLabel style={!(this.state.reCheck)?styles.redBox:styles.normal}>
                            {(this.state.reCheck)?<Label><Text style={{color:"green"}}>Passwords Match</Text></Label>:<Label style={{color:"red"}}>Confirm password : Passwords do not match</Label>}
                            <Input secureTextEntry autoCorrect={false} onChangeText={(text)=>this.checkPassword(text)} autoCapitalize="none" />
                        </Item>                  
                        <Item floatingLabel>
                            <Label>Mobile Number</Label>
                            <Input  onChangeText={(text)=>this.handleMobile(text)} />
                        </Item>
                        <Item floatingLabel>
                            <Label>Email Address</Label>
                            <Input autoCorrect={false} autoCapitalize="none" onChangeText={(text)=>this.handleEmail(text)} />
                        </Item>
                        <Button onPress={()=>this.handleSubmit()} style={styles.nextButton} full disabled={!this.state.flag}><Text style={styles.buttonText}>Next</Text></Button>

                        <View style={styles.infoText}>
                            <Text>By clicking on Next, you will agree to our terms and conditions. You will be asked about your business details to complete the signup process.</Text>
                        </View>                        
                    </Form>
                </Content>
            </Container>
        )
    }

}

const styles = StyleSheet.create({
    redCheck:{
        color: "red"
    },
    infoText:{
        marginTop: 10,
        padding: 5
    },
    nextButton:{
        marginTop:30
    },
    signupScreen:{
        backgroundColor: "#F0F3F4"
    },
    buttonText: {
        color:"white"
    }
})

const mapStateToProps = (state) =>{
    return {
        auth: state.authReducer
    }
}

const mapDispatchToProps = (dispatch) =>{
    return {
        setToken: (token)=>{
            dispatch({
                type: "SIGNIN",
                payload: token
            })
        }
    }
}

export default connect(mapStateToProps,mapDispatchToProps)(SignupScreen)

代码 App.js

import React from 'react';
import AppContainer from './Utils/Routes';
import {createStore} from 'redux';
import {Provider} from 'react-redux';

const authState = {
  token: null
}


const authReducer = (state=authState, action) =>{
  switch(action.type){
      case "SIGNIN":{
          state = {
              ...state,
              token: action.payload
          };
          break;
      }
  }
  return state;
};

const store = createStore(authReducer);

store.subscribe(()=>{

});

class App extends React.Component{
  render(){
    return(
      <Provider store={store}>
        <AppContainer />
      </Provider>
    )
  }
}

export default App;

【问题讨论】:

    标签: react-native redux react-redux react-navigation


    【解决方案1】:

    你从状态中得到了错误的值,

    const mapStateToProps = (state) =>{
        return {
            auth: state.authReducer
        }
    }
    

    你的状态是这样的,

    const authState = {
      token: null
    }
    

    您正在尝试访问状态中不存在的state.authReducer。所以你应该访问token

    const mapStateToProps = (state) =>{
        return {
            auth: state.token
        }
    }
    

    你应该像这样使用auth

    if (this.props.auth !== "" || this.props.auth !== null) this.props.navigation.navigate("LoginScreen")
    

    【讨论】:

    • 是的。这解决了我的问题。万分感谢。我的错误,我在使用 combinereducers() 时使用的语法,并且在这段代码中使用了单个 reducer。
    猜你喜欢
    • 2021-04-28
    • 1970-01-01
    • 2016-09-19
    • 2018-09-20
    • 1970-01-01
    • 2019-07-12
    • 2017-02-19
    • 1970-01-01
    • 2020-06-21
    相关资源
    最近更新 更多