微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

React useCallback - 我无法更新我的函数

如何解决React useCallback - 我无法更新我的函数

我想更新我的回调函数

const getSchema = React.useCallback(
        () => {
            const schema = twSchema(
                labels,isInitialWarehouseActive ? initialWarehouse.id : undefined,operationTypedisabled ? initialWarehouse.operation_type : undefined
            );
            schema.addValidator((model,_schema) => {
                if (model.daterangeMode && (!model.timeRangeMode || model.hasRampHours) && !model.dateInpu.to) {
                    _schema.setModelError('dateInput.to',labels.fieldisrequired);
              ...
              ...
            });
            return schema;
        },[initialStore]
    );

哪里 twSchema:

const twSchema = (labels,initialStoreId,storeOperationType) => new Schema({...

以及我的 getSchema 用例:

<Form
                key="time-window-form"
                ctx="time-window-form"
                eventsEmitter={eventsEmitter}
                model={model}
                onError={onError}
                onSubmit={(data) => {
                   ...
                    }).then(() => {
                       ...
                    })
                        .catch(hint => displayMessageAndHighlightValidatedComponent(hint));
                }}
                schema={getSchema()}
            >

我将此值 (getSchema) 用于我的表单(我必须为我的表单设置架构)。

根据可能发生的错误,我想向我的架构添加一些验证器,但我不能:

 const displayMessageAndHighlightValidatedComponent = (hint) => {
        getSchema().addValidator((model,schema) => {
//this code is not executed!!!
            console.log(schema);
            console.log('SCHEMA');
            schema.setModelError('dateInputField',labels.createNextTimeWindow);
        });
        return onFailed();
    };

问题是为什么?为什么我不能更新我的对象/函数?我必须删除 useCallback 才能动态添加验证器...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。