Redux:useDispatch之后,useSelector不会更新状态

时间:2020-10-28 10:02:36

标签: javascript react-redux

import { useSelector, useDispatch } from 'react-redux';    

const dispatch = useDispatch();
const settingState = useSelector((state: RootState) => state.setting);

const _onEmailEdit = (value: string) => {
     dispatch(updateField('email', value.trim()));
}

<TextInput
      placeholder="Enter Email"
      keyboardType="email-address"
      textContentType="emailAddress"
      autoCompleteType="email"
      autoCapitalize={'none'}
      value={settingState.userDetails.email || ''}
      onChangeText={_onEmailEdit}
      autoFocus
    />

还有调度功能...

export const updateField = (
    fieldName: Setting.UpdatableFieldName,
    value: string,
) => ({
    type: Setting.UPDATE_FIELD,
    payload: {
        fieldName,
        value,
   },
});




  return {
    ...state,
    userDetails: {
      ...state.userDetails,
      [fieldName]: value,
    },
    errorMessage: '',
  };

TextInput在屏幕上发生了变化,但是当我关闭然后重新打开屏幕时,useSelector的值是相同的。

如何使用分派设置状态?谢谢。

0 个答案:

没有答案