gpt4 book ai didi

javascript - 使用带有钩子(Hook)的 React.memo 来控制输入

转载 作者:行者123 更新时间:2023-11-30 11:04:15 25 4
gpt4 key购买 nike

我正在通过自定义 React Hook 提供一些表单功能。这个钩子(Hook)有一些类似于 Formik 的功能(但这是非常基本的东西)。

function useFormValidation(initialState, validate) {
const [values, setValues] = React.useState(initialState);
const [errors, setErrors] = React.useState({});
const [isSubmitting, setSubmitting] = React.useState(false);

React.useEffect(() => {
if (isSubmitting) {
const noErrors = Object.keys(errors).length === 0;
if (noErrors) {
console.log("authenticated!", values.email, values.password);
setSubmitting(false);
} else {
setSubmitting(false);
}
}
}, [errors]);

function handleChange(event) {
setValues({
...values,
[event.target.name]: event.target.value
});
}

function handleBlur() {
const validationErrors = validate(values);
setErrors(validationErrors);
}

function handleSubmit(event) {
event.preventDefault();
const validationErrors = validate(values);
setErrors(validationErrors);
setSubmitting(true);
}

return {
handleSubmit,
handleChange,
handleBlur,
values,
errors,
isSubmitting
};
}



表格如下:

function Register() {
const {
handleSubmit,

handleChange,
handleBlur,
values,
errors,
isSubmitting
} = useFormValidation(INITIAL_STATE, validateAuth);
// const [email, setEmail] = React.useState("");
// const [password, setPassword] = React.useState("");

return (
<div className="container">
<h1>Register Here</h1>
<form onSubmit={handleSubmit}>
<Input
handleChange={handleChange}
handleBlur={handleBlur}
name="email"
value={values.email}
className={errors.email && "error-input"}
autoComplete="off"
placeholder="Your email address"
/>
{errors.email && <p className="error-text">{errors.email}</p>}
<Input
handleChange={handleChange}
handleBlur={handleBlur}
value={values.password}
className={errors.password && "error-input"}
name="password"
// type="password"
placeholder="Choose a safe password"
/>
{errors.password && <p className="error-text">{errors.password}</p>}
<div>
<button disabled={isSubmitting} type="submit">
Submit
</button>
</div>
</form>
</div>
);
}

下一个是内存组件:

function Input({
handleChange,
handleBlur,
name,
value,
className,
autoComplete,
placeholder,
type
}) {

return (
<input
onChange={handleChange}
onBlur={handleBlur}
name={name}
value={value}
className={className}
autoComplete={autoComplete}
placeholder={placeholder}
type={type}
/>
);
}

function areEqual(prevProps, nextProps) {
console.log(`
prevProps: ${JSON.stringify(prevProps.value)}
nextProps: ${JSON.stringify(nextProps.value)}
`);
return prevProps.value === nextProps.value;
}
const useMemo = (component, propsAreEqual) => {
return memo(component, propsAreEqual);
};
export default useMemo(Input, areEqual);

我在第一个输入中输入了一些文本。然后,当我切换到第二个输入并开始输入时,第一个输入失去了值(value)。就像表单没有呈现最后的 MEMOIZED 输入,而是呈现以前的版本。我是 React 初学者,无法找出解决方案。有什么帮助吗?

最佳答案

尝试使用带有函数的 setState 更新器形式:

function handleChange(event) {
// event.target wont be available when fn is run in setState
// so we save them in our own local variables here
const { name, value } = event.target;

setValues(prev => ({
...prev,
[name]: value
}));
}

关于javascript - 使用带有钩子(Hook)的 React.memo 来控制输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56319349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com