[英]Can someone explain me how custom hooks get the data and in depth flow of custom hooks
//use Input HOOK
我想知道这个自定义挂钩是如何工作的
import { useState } from "react";
export default initialValue => {
const [value, setValue] = useState(initialValue);
return {
value,
onChange: event => {
setValue(event.target.value);
},
reset: () => setValue("")
};
};
//todo form
这个 onchange 方法如何工作它如何更新数据,即使没有 onchange function 被写入这个程序
import React from "react";
import TextField from "@material-ui/core/TextField";
import useInputState from "./useInputState";
const TodoForm = ({ saveTodo }) => {
const { value, reset, onChange } = useInputState("");
return (
<form
onSubmit={event => {
event.preventDefault();
saveTodo(value);
reset();
}}
>
<TextField
variant="outlined"
placeholder="Add todo"
margin="normal"
value={value}
onChange={onChange}
/>
</form>
);
};
export default TodoForm;
JS 中的函数被视为任何其他变量。 因此, de-structured onChange
(在组件中)正在引用custom hook
中匿名定义的 function,然后由TextField
组件的 onChange 方法使用。 这类似于你通过引用传递变量的方式是 JS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.