小编Kri*_*ado的帖子

在 React Hook Form 中设置默认值

我想检查 RHF 是否用户已使用参数修改了表单值isDirty

手动设置默认值是可行的:

const {handleSubmit, control, setValue, setError, setFocus} = useForm({defaultValues: {name: ""}});
Run Code Online (Sandbox Code Playgroud)

这似乎工作正常。

但是,当用户尝试编辑表单时,我将值加载到表单中setValue

现在我不知道如何以编程方式设置默认值。

如何使用 useEffect() 更改 React-Hook-Form defaultValue?

我就是这样做的,但答案并不正确。值已设置,但 defaultValues 不会以这种方式更改,因此 RHF 无法进行比较。

确保在 useForm 中提供所有输入的默认值,以便钩子表单可以有单一的事实来源来比较表单是否脏。

https://react-hook-form.com/api/useform/formstate

如何动态设置 defaultValues 以便它甚至可以在“编辑”模式下工作?

reactjs react-hook-form

8
推荐指数
1
解决办法
5万
查看次数

自定义 React Router 提示:历史记录一次仅支持一个提示

我创建了一个自定义的 React Router Prompt 来显示用户何时想要导航到其他页面并且尚未保存数据。

一切似乎都按预期进行,但我有一个恼人的警告:

Warning: A history supports only one prompt at a time
Run Code Online (Sandbox Code Playgroud)

该组件如下所示:

export function RouterPrompt(props) {
    const {when, onOK, onCancel, title} = props;

    const history = useHistory();

    const [showPrompt, setShowPrompt] = useState(false);
    const [currentPath, setCurrentPath] = useState("");

    useEffect(() => {
        if (when) {
            history.block((prompt) => {
                setCurrentPath(prompt.pathname);
                setShowPrompt(true);
                return "true";
            });
        } else {
            history.block(() => {
            });
        }
    }, [when]);

    const handleOK = useCallback(async () => {
        if (onOK) {
            const canRoute = await …
Run Code Online (Sandbox Code Playgroud)

reactjs react-router material-ui

7
推荐指数
1
解决办法
8550
查看次数