useImperativeHandle 钩子不更新值
useImperativeHandle hook does not update the value
我在我的应用程序中使用 useImperativeHandle 挂钩来为父组件提供值访问权限:
const [phone,setPhone]=useState("");
useImperativeHandle(
ref,
() => ({
type: "text",
name: "phone",
value: phone
}),
[phone]
);
当我使用 setPhone 更新 phone 时,值不会更新。我的实施有什么问题?
useImperativeHandle 需要让组件使用 forwardRef
,一旦你这样做了,你将能够访问父级中更新的 ref,因为你提供 phone
作为它的依赖项。
import React, {
useEffect,
useState,
useImperativeHandle,
forwardRef,
useRef
} from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const App = forwardRef((props, ref) => {
const [phone, setPhone] = useState("");
useImperativeHandle(
ref,
() => ({
type: "text",
name: "phone",
value: phone
}),
[phone]
);
useEffect(() => {
setTimeout(() => {
setPhone("9898098909");
}, 3000);
}, []);
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
});
const Parent = () => {
const appRef = useRef(null);
const handleClick = () => {
console.log(appRef.current.value);
};
return (
<>
<App ref={appRef} />
<button onClick={handleClick}>Click</button>
</>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<Parent />, rootElement);
如果你很懒惰 and/or 优化在你的应用程序中还不是特别重要,你可以将 [{}]
的依赖项传递给 useImperativeHandle()
以在每次组件重新启动时更新-呈现,确保值始终是最新的。
const App = forwardRef((props, ref) => {
const [phone, setPhone] = useState("");
useImperativeHandle(
ref,
() => ({
type: "text",
name: "phone",
value: phone,
// other values that you don't have to keep track of via dependency list
}),
[{}]
);
我在我的应用程序中使用 useImperativeHandle 挂钩来为父组件提供值访问权限:
const [phone,setPhone]=useState("");
useImperativeHandle(
ref,
() => ({
type: "text",
name: "phone",
value: phone
}),
[phone]
);
当我使用 setPhone 更新 phone 时,值不会更新。我的实施有什么问题?
useImperativeHandle 需要让组件使用 forwardRef
,一旦你这样做了,你将能够访问父级中更新的 ref,因为你提供 phone
作为它的依赖项。
import React, {
useEffect,
useState,
useImperativeHandle,
forwardRef,
useRef
} from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const App = forwardRef((props, ref) => {
const [phone, setPhone] = useState("");
useImperativeHandle(
ref,
() => ({
type: "text",
name: "phone",
value: phone
}),
[phone]
);
useEffect(() => {
setTimeout(() => {
setPhone("9898098909");
}, 3000);
}, []);
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
});
const Parent = () => {
const appRef = useRef(null);
const handleClick = () => {
console.log(appRef.current.value);
};
return (
<>
<App ref={appRef} />
<button onClick={handleClick}>Click</button>
</>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<Parent />, rootElement);
如果你很懒惰 and/or 优化在你的应用程序中还不是特别重要,你可以将 [{}]
的依赖项传递给 useImperativeHandle()
以在每次组件重新启动时更新-呈现,确保值始终是最新的。
const App = forwardRef((props, ref) => {
const [phone, setPhone] = useState("");
useImperativeHandle(
ref,
() => ({
type: "text",
name: "phone",
value: phone,
// other values that you don't have to keep track of via dependency list
}),
[{}]
);