简体   繁体   中英

Redux reducer can't update redux state/props

I'm struggling to update props/state from redux with TextInput(ReactNative). I can get actions and initial state from redux to SignIn component, however I can't update email props(Input value). I searched for a solution but I can't find one that solves my problem.

SignInScreen component receive actions in props and I can access AuthReducer from the component so I guess Redux structure is fine. Only in AuthReducer.js, return {...state, email: action.payload} doesn't update props.

Following is my code. I've posted the important parts, but I will add more if needed.

AuthReducer.js

// I can access this reducer
// INITIAL_STATE will be shown TextInput components

import {EMAIL_CHANGED } from '../actions/actionTypes';
import { AlertIOS } from 'react-native';

const INITIAL_STATE = { email: 'this is shown' }

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case EMAIL_CHANGED:

      // This alert function works with inputted payload(email)
      AlertIOS.alert('Input', action.payload);

      // This return doesn't update props.email in SignInScreen Component
      return { ...state, email: action.payload };

    default:
      return state;
  }
}

configureStore.js

import AuthReducer from './reducers/AuthReducer';
import { createStore, combineReducers } from 'redux';

const rootReducer = combineReducers({
    auth: AuthReducer
});

const configureStore = () => {
    return createStore(rootReducer);
};

export default configureStore;

SignInScreen.js

import { connect } from 'react-redux';
import { emailChanged } from '../../store/actions';
import etc...

onEmailChange = (text) => {
  this.props.emailChanged(text);
};

render() {
  return (
    <View>
      <View>
        <Text>Email:</Text>
        <TextInput
          placeholder="email@mail.com"
          autoCorrect={false}
          value={this.props.email}
          onChangeText={email => this.onEmailChange(email)}
        />
      </View>
    </View>
  );
}

const mapStateToProps = state => {
  return {
    email: state.auth.email
  };
};

export default connect(mapStateToProps, { emailChanged })(SignInScreen);

App.js

import etc...

  render() {
    return (
      <Provider store={store}>
        <AppNavigator 
          style={styles.container} //Stephen Girder course said this was good practice
                                //to ensure fills screen with flex: 1 
        />
      </Provider>
    );
  }

const store = configureStore();

export default App;

package.json

"expo": "^32.0.0",
"react": "^16.8.6",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
"react-dom": "^16.8.6",
"react-navigation": "^3.10.2",
"react-redux": "^6.0.1",
"redux": "^4.0.1",

在此处输入图片说明

You forgot to dispatch the action. Try using the following in SignInScreen.js :

const mapStateToProps = state => {
  return {
    email: state.auth.email
  };
};

const mapDispatchToProps = dispatch => {
  return {
      emailChanged: value => dispatch(emailChanged(value))
  }
};

export default connect(mapStateToProps, mapDispatchToProps)(SignInScreen);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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