Skip to content

Commit 35ac597

Browse files
committed
execute-child-function-from-parent
1 parent ee40c2a commit 35ac597

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
A full example
2+
[Source](https://stackblitz.com/edit/react-a1mlqw?file=index.js)
3+
4+
```js
5+
import React, { Component, useRef, useImperativeHandle } from "react"
6+
import { render } from "react-dom"
7+
import Hello from "./Hello"
8+
import "./style.css"
9+
10+
class App extends Component {
11+
constructor() {
12+
super()
13+
this.state = {
14+
name: "React",
15+
}
16+
}
17+
18+
render() {
19+
return (
20+
<div>
21+
<Hello name={this.state.name} />
22+
<p>Start editing to see some magic happen :)</p>
23+
<Parent />
24+
</div>
25+
)
26+
}
27+
}
28+
29+
function Parent(props) {
30+
const refs = useRef([])
31+
const someData = [1, 2, 3, 4, 5]
32+
33+
function validateChildren() {
34+
refs.current.forEach(child => {
35+
// in below the validate() function comes from Child comp and will be available to parent component using ref
36+
child.validate()
37+
})
38+
}
39+
40+
return (
41+
<>
42+
<button onClick={validateChildren}>Validate</button>
43+
{someData.map((data, index) => {
44+
return <Child key={index} ref={ins => (refs.current[index] = ins)} />
45+
})}
46+
</>
47+
)
48+
}
49+
50+
const Child = React.forwardRef((props, ref) => {
51+
useImperativeHandle(ref, () => ({
52+
validate() {
53+
// to validate this component
54+
console.log("I'm clicked")
55+
},
56+
}))
57+
58+
return <>Some code here</>
59+
})
60+
61+
render(<App />, document.getElementById("root"))
62+
```
63+
64+
From the docs,
65+
66+
useImperativeHandle customizes the instance value that is exposed to parent components when using ref. As always, imperative code using refs should be avoided in most cases. useImperativeHandle should be used with forwardRef
67+
68+
**Another example of passing ref further down Parent > Middle > Child want to call a method from Child in Parent**
69+
[https://stackoverflow.com/questions/57389181/unable-to-call-child-function-from-parent-using-refs-with-functional-component](https://stackoverflow.com/questions/57389181/unable-to-call-child-function-from-parent-using-refs-with-functional-component)

React/refs-in-react/refs-in-React.md

+11
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ The short answer is that most of the time you can safely use the createRef API.
130130

131131
**Note, You can't use createRef for pure functional components since they lack many of the React-y features like state & lifecycle components**
132132

133+
#### Finally, as a rule of thumb:
134+
135+
- Don’t overuse refs
136+
- Abolish string refs
137+
- Use callback refs when you have to dynamically set them
138+
- When in a class component, use createRef in all other cases
139+
- When in a function component, use useRef in all other cases
140+
- Use forwardRef when you need access to a child ref
141+
- Use Hooks to empower your function component
142+
- If the child ref must not be a function component, then use a custom method to trigger focus programmatically from the parent (remember you will get a component instance, not a DOM element)
143+
133144
#### Further Reading
134145

135146
[https://moduscreate.com/blog/everything-you-need-to-know-about-refs-in-react/](https://moduscreate.com/blog/everything-you-need-to-know-about-refs-in-react/)

0 commit comments

Comments
 (0)