[英]Can someone explain me how custom hooks get the data and in depth flow of custom hooks
//use Input HOOK
I want to know that how this custom hook work我想知道这个自定义挂钩是如何工作的
import { useState } from "react";
export default initialValue => {
const [value, setValue] = useState(initialValue);
return {
value,
onChange: event => {
setValue(event.target.value);
},
reset: () => setValue("")
};
};
//todo form
How this onchange method work how it update the data even though no onchange function is write in this programm这个 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;
Functions in JS are treated like any other variable. JS 中的函数被视为任何其他变量。 So the
de-structured onChange
(in the component) is taking the reference for a function which is defined anonymously in the custom hook
, which is then used by the onChange method of the TextField
component.因此,
de-structured onChange
(在组件中)正在引用custom hook
中匿名定义的 function,然后由TextField
组件的 onChange 方法使用。 This is similar to how you pass variables by reference is JS.这类似于你通过引用传递变量的方式是 JS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.