在 React-Hooks UseRef 中我无法获得新的状态值
In React-Hooks UseRef i cant get the new state value
我正在尝试使用 react-hooks 的 useref,但是当我调用 setAccountVal 方法时,尽管当我提醒该值时子状态发生变化,它仍然是 "Ege"。你对这个案子有什么想法吗?
import React, { useState, useRef, useImperativeHandle, useEffect, forwardRef } from "react";
interface A {
name?: string;
}
const Child = forwardRef((props: A, ref) => {
const [acc, setAcc] = useState("Ege");
useImperativeHandle(ref, () => ({
getAlert(value: string) {
alert("getAlert from Child===>" + value);
},
setAccountVal(val: string) {
setAcc(val);
},
acc
}));
useEffect(
() => {
debugger;
setAcc(props.name || "dummy");
console.log(acc + " --");
},
[props.name]
);
const changeState = (e:React.FormEvent<HTMLInputElement>)=>{
setAcc(e.currentTarget.value);
}
return <input onChange={changeState}></input>;
})
function App() {
let [name, setName] = useState("Nate");
let nameRef = useRef<any>(null);
const submitButton = () => {
debugger;
const comp = nameRef.current;
comp.setAccountVal("Mahmut");
comp.getAlert(comp.acc);
};
return (
<div className="App">
<p>{name}</p>
<div>
<button type="button" onClick={submitButton}>
Submit
</button>
<Child ref={nameRef} />
</div>
</div>
);
}
export default App;
当我调用普通方法时,我可以看到我想要的。但问题是我可以在 acc 状态上看到新的状态值,但是当我从父级那里获取它时,它显示的是旧的(实际上是初始状态 val ("Ege"))状态值。
Edit : Actually what i want to do is below
//Child component with forwardRef
const [account, setAccount] = useState("defaultAccount");
const [branch, setBranch] = useState("defaultBranch");
const [person, setPerson] = useState("defaultPerson");
useImperativeHandle(ref, () => ({
selectedAccount: account,
selectedBranch: branch,
selectedPerson: person
}));
//Parent component
const childComponentRef = useRef(null);
//Is it possible to change child component's state? Is it allowed?
childComponentRef.current.selectedAccount = "new account";
childComponentRef.current.selectedBranch = "new branch";
childComponentRef.current.selectedPerson = "new person";
alert(childComponentRef.current.selectedAccount);
alert(childComponentRef.current.selectedBranch);
alert(childComponentRef.current.selectedPerson);
//We can change these variables but child component does not rerender with new values.
Ok! I've found the solution. When i use the ref variables with getter setter now i can set also get the current values with useRef!!
//Child component with forwardRef
const [account, setAccount] = useState("defaultAccount");
const [branch, setBranch] = useState("defaultBranch");
const [person, setPerson] = useState("defaultPerson");
useImperativeHandle(ref, () => ({
**get account(){
return account;
},
set account(val : string){
setAccount(val);
},**
selectedBranch: branch,
selectedPerson: person
}));
//Parent component
const childComponentRef = useRef(null);
//Is it possible to change child component's state? Is it allowed?
childComponentRef.current.selectedAccount = "new account";
childComponentRef.current.selectedBranch = "new branch";
childComponentRef.current.selectedPerson = "new person";
alert(childComponentRef.current.selectedAccount);
alert(childComponentRef.current.selectedBranch);
alert(childComponentRef.current.selectedPerson);
//We can change these variables but child component does not rerender with new values
详细演示:
这是因为您在使用 useImperativeHandle 时没有设置任何输入,所以函数永远不会更新。只需添加 acc 作为输入,它就会起作用:
useImperativeHandle(ref, () => ({
getAlert(value: string) {
alert("getAlert from Child===>" + value);
},
setAccountVal(val: string) {
setAcc(val);
},
acc
}), [acc]);
我正在尝试使用 react-hooks 的 useref,但是当我调用 setAccountVal 方法时,尽管当我提醒该值时子状态发生变化,它仍然是 "Ege"。你对这个案子有什么想法吗?
import React, { useState, useRef, useImperativeHandle, useEffect, forwardRef } from "react";
interface A {
name?: string;
}
const Child = forwardRef((props: A, ref) => {
const [acc, setAcc] = useState("Ege");
useImperativeHandle(ref, () => ({
getAlert(value: string) {
alert("getAlert from Child===>" + value);
},
setAccountVal(val: string) {
setAcc(val);
},
acc
}));
useEffect(
() => {
debugger;
setAcc(props.name || "dummy");
console.log(acc + " --");
},
[props.name]
);
const changeState = (e:React.FormEvent<HTMLInputElement>)=>{
setAcc(e.currentTarget.value);
}
return <input onChange={changeState}></input>;
})
function App() {
let [name, setName] = useState("Nate");
let nameRef = useRef<any>(null);
const submitButton = () => {
debugger;
const comp = nameRef.current;
comp.setAccountVal("Mahmut");
comp.getAlert(comp.acc);
};
return (
<div className="App">
<p>{name}</p>
<div>
<button type="button" onClick={submitButton}>
Submit
</button>
<Child ref={nameRef} />
</div>
</div>
);
}
export default App;
当我调用普通方法时,我可以看到我想要的。但问题是我可以在 acc 状态上看到新的状态值,但是当我从父级那里获取它时,它显示的是旧的(实际上是初始状态 val ("Ege"))状态值。
Edit : Actually what i want to do is below
//Child component with forwardRef
const [account, setAccount] = useState("defaultAccount");
const [branch, setBranch] = useState("defaultBranch");
const [person, setPerson] = useState("defaultPerson");
useImperativeHandle(ref, () => ({
selectedAccount: account,
selectedBranch: branch,
selectedPerson: person
}));
//Parent component
const childComponentRef = useRef(null);
//Is it possible to change child component's state? Is it allowed?
childComponentRef.current.selectedAccount = "new account";
childComponentRef.current.selectedBranch = "new branch";
childComponentRef.current.selectedPerson = "new person";
alert(childComponentRef.current.selectedAccount);
alert(childComponentRef.current.selectedBranch);
alert(childComponentRef.current.selectedPerson);
//We can change these variables but child component does not rerender with new values.
Ok! I've found the solution. When i use the ref variables with getter setter now i can set also get the current values with useRef!!
//Child component with forwardRef
const [account, setAccount] = useState("defaultAccount");
const [branch, setBranch] = useState("defaultBranch");
const [person, setPerson] = useState("defaultPerson");
useImperativeHandle(ref, () => ({
**get account(){
return account;
},
set account(val : string){
setAccount(val);
},**
selectedBranch: branch,
selectedPerson: person
}));
//Parent component
const childComponentRef = useRef(null);
//Is it possible to change child component's state? Is it allowed?
childComponentRef.current.selectedAccount = "new account";
childComponentRef.current.selectedBranch = "new branch";
childComponentRef.current.selectedPerson = "new person";
alert(childComponentRef.current.selectedAccount);
alert(childComponentRef.current.selectedBranch);
alert(childComponentRef.current.selectedPerson);
//We can change these variables but child component does not rerender with new values
详细演示:
这是因为您在使用 useImperativeHandle 时没有设置任何输入,所以函数永远不会更新。只需添加 acc 作为输入,它就会起作用:
useImperativeHandle(ref, () => ({
getAlert(value: string) {
alert("getAlert from Child===>" + value);
},
setAccountVal(val: string) {
setAcc(val);
},
acc
}), [acc]);