Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialState参数很多,如何一次性setState更新? #28

Open
gikkkkkk opened this issue Jul 26, 2022 · 3 comments
Open

initialState参数很多,如何一次性setState更新? #28

gikkkkkk opened this issue Jul 26, 2022 · 3 comments
Labels
question Further information is requested

Comments

@gikkkkkk
Copy link

目前做法是这样的,感觉多余代码太多了。求指教

export const Model = defineModel("global", {
initialState: {
account: "",
currentEnv: "",
license: "",
loginStatus: false,
pwd: "",
rememberPwdFlag: false,
system: "",
userInfo: {} as UserInfoSuccessPayload,
},
actions: {
// 重置
reset() {
return this.initialState;
},
// 设置登录状态
setLoginStatus(state, status) {
state.loginStatus = status;
},
// 设置当前环境
setCurrentEnv(state, env) {
state.currentEnv = env;
},
// 设置账号
setAccount(state, account: string) {
state.account = account;
},
// 设置密码
setPwd(state, pwd: string) {
state.pwd = pwd;
},
// 设置是否记住密码
setRememberPwdFlag(state, rememberPwdFlag) {
state.rememberPwdFlag = rememberPwdFlag;
},
// 设置系统
setSystem(state, system) {
state.system = system;
},
// 设置用户信息
setUserInfo(state, userInfo) {
state.userInfo = userInfo;
},
},
});

@geekact
Copy link
Member

geekact commented Jul 26, 2022

const initialState = {...};

type State = typeof initialState;

export const globalModel  = defineModel('global', {
  initialState,
  effects: {
    patchUpdate(data: Partial<State>) {
      this.setState(data);
    }
  }
});

effects里的setState支持部分数据更新,类似 React 的 setState

@gikkkkkk
Copy link
Author

谢谢 还有两个问题想请教下。
1、更细state时卡住了
const Screen: React.FC = () => {

const globalModel = useModel(GlobalModel);
	// 这边是可以生效的,GlobalModel中的rememberPwdFlag更新了
const changeCheck = (isCheck: boolean) => {
    GlobalModel.patchUpdate({ rememberPwdFlag: isCheck });
};

const { errors, touched, values, isValid, handleSubmit, setFieldValue, setFieldTouched } = useFormik<AuthForm>({
    validateOnMount: true,
    validationSchema: AuthValidation,
    initialValues: { account: globalModel.account, password: globalModel.pwd },
    onSubmit: async (values) => {
        if (isValid) {
									// 页面按钮点击了,会卡在这个地方,不会执行下去了
                GlobalModel.patchUpdate({ account: values.account, pwd: values.password });
            await GlobalModel.verifyPassword(values.account, values.password);
        }
    },
});

return (
    <></>
);

};

export default LoginScreen;

2、modelA可以更新modelB的数据吗?

@geekact
Copy link
Member

geekact commented Jul 26, 2022

  1. 看代码没有发现问题,可以看看控制台输出以及打印日志来排查。或者建一个最简单的repo方便我协助排查

  2. 如何在modelA里访问modelB的数据? #18

@geekact geekact added the question Further information is requested label Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants