javascript - Redux reducer 无法更新 redux state/props

标签 javascript react-native redux react-redux

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

SignInScreen 组件在 props 中接收操作,我可以从组件访问 AuthReducer,所以我猜 Redux 结构没问题。仅在 AuthReducer.js 中,返回 {...state, email: action.payload} 不会更新 props。

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

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;

登录屏幕.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;

包.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",

enter image description here

最佳答案

您忘记发送操作。 尝试在 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);

关于javascript - Redux reducer 无法更新 redux state/props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56911224/

相关文章:

javascript - 如何让嵌套的CSS元素的宽度都一样?

react-native - 在 babel 构建阶段传递环境变量以导入不同的文件

javascript - mapDispatchToProps 未将 actionCreators 分派(dispatch)到所需组件

reactjs - 何时使用原生 React.useReducer Hook 以及它与 Redux 的区别

javascript - MERN 堆栈 : Using Redux to store whether user is logged in or not

php - 使用 PHP 将参数传递给 Javascript

javascript - 需要有关文本扩展器 jQuery 插件的帮助

javascript - 如何从 Simile Timeline 中的 band 中删除日期

android - 在 Android 设备中运行 Expo/React Native

javascript - 在功能组件初始渲染时将变量添加到存储中