gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:25:55 25 4
gpt4 key购买 nike

我正在努力使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com