Redux Reducer无法更新Redux状态/道具

时间:2019-07-06 04:28:01

标签: javascript react-native redux react-redux

我正在努力使用TextInput(ReactNative)从redux更新道具/状态。 我可以从redux到SignIn组件获取操作和初始状态,但是我无法更新电子邮件道具(输入值)。 我搜索了一种解决方案,但找不到能解决我问题的解决方案。

rest组件接受道具中的动作,并且我可以从该组件访问AuthReducer,所以我认为Redux结构很好。仅在AuthReducer.js中,返回{...状态,电子邮件:action.payload}不会更新道具。

以下是我的代码。 我已经发布了重要的部分,但是如果需要,我会添加更多。

AuthReducer.js

SignInScreen

configureStore.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;
  }
}

SignInScreen.js

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

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

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

export default configureStore;

App.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);

package.json

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;

enter image description here

1 个答案:

答案 0 :(得分:1)

您忘记了分派动作。 尝试在 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);
相关问题