admin管理员组文章数量:1406178
I am having a parent and child ponent the looks like this
export function Parent(){
function handleClick(){
// Here I need to access Child ponents ref, that may be taken to this function as a parameter
}
return(
<>
<h1> Parent ponent here </h1>
<Child/>
<button onClick={handleClick}>Get Ref</button>
</>
)
}
export function Child(){
const childRef = useRef("test")
return(
<h1> Child Component</h1>
)
}
How do I get the ref that is defined inside the child ponent to be accessed inside the parent based on the button click in the parent ponent?
I am having a parent and child ponent the looks like this
export function Parent(){
function handleClick(){
// Here I need to access Child ponents ref, that may be taken to this function as a parameter
}
return(
<>
<h1> Parent ponent here </h1>
<Child/>
<button onClick={handleClick}>Get Ref</button>
</>
)
}
export function Child(){
const childRef = useRef("test")
return(
<h1> Child Component</h1>
)
}
How do I get the ref that is defined inside the child ponent to be accessed inside the parent based on the button click in the parent ponent?
Share Improve this question asked Mar 8, 2022 at 15:06 user16860065user16860065 1- Set the useRef hook in your parent ponent in the <Child> tag – SimonDX Commented Mar 8, 2022 at 15:24
2 Answers
Reset to default 3You can use React.forwardRef to get a hold of the child ref in your parent ponent.
export function Parent(){
const childRef = useRef();
function handleClick(){
console.log(childRef.current);
}
return(
<>
<h1> Parent ponent here </h1>
<Child ref={childRef} />
<button onClick={handleClick}>Get Ref</button>
</>
)
}
export default React.forwardRef((props, ref) => {
return(
<h1 ref={ref}>Child Component</h1>
)
});
Alternatively, if you don't like to use React.forwardRef
, you can also just pass the ref as prop. But you can't name it ref
in this case.
export function Parent(){
const childRef = useRef();
function handleClick(){
console.log(childRef.current);
}
return(
<>
<h1> Parent ponent here </h1>
<Child innerRef={childRef} />
<button onClick={handleClick}>Get Ref</button>
</>
)
}
export default function Child({ innerRef }) {
return(
<h1 ref={innerRef}>Child Component</h1>
)
});
Could you please try that. I passed function as props to child and set ref there when child rendered;
export function Parent(){
function handleClick(value){
var childRef = value;
}
return(
<>
<h1> Parent ponent here </h1>
<Child handleClick={handleClick}/>
<button onClick={handleClick}>Get Ref</button>
</>
)
}
export function Child({handleClick}){
const childRef = useRef("test")
handleClick(childRef)
return(
<h1> Child Component</h1>
)
}
本文标签: javascriptAccess child components ref inside parent component in functional componentStack Overflow
版权声明:本文标题:javascript - Access child components ref inside parent component in functional component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744973150a2635355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论