We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
目前做法是这样的,感觉多余代码太多了。求指教
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; }, }, });
The text was updated successfully, but these errors were encountered:
const initialState = {...}; type State = typeof initialState; export const globalModel = defineModel('global', { initialState, effects: { patchUpdate(data: Partial<State>) { this.setState(data); } } });
effects里的setState支持部分数据更新,类似 React 的 setState
effects
Sorry, something went wrong.
谢谢 还有两个问题想请教下。 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的数据吗?
看代码没有发现问题,可以看看控制台输出以及打印日志来排查。或者建一个最简单的repo方便我协助排查
如何在modelA里访问modelB的数据? #18
No branches or pull requests
目前做法是这样的,感觉多余代码太多了。求指教
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;
},
},
});
The text was updated successfully, but these errors were encountered: