CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2021-04-27
by Utonium

Original Post

Original - Posted on 2016-06-21
by rossipedia



            
Present in both answers; Present only in the new answer; Present only in the old answer;



<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const { forwardRef, useRef, useImperativeHandle } = React;
// We need to wrap component in `forwardRef` in order to gain // access to the ref object that is assigned using the `ref` prop. // This ref is passed as the second parameter to the function component. const Child = forwardRef((props, ref) => {
// The component instance will be extended // with whatever you return from the callback passed // as the second argument useImperativeHandle(ref, () => ({
getAlert() { alert("getAlert from Child"); }
}));
return <h1>Hi</h1>; });
const Parent = () => { // In order to gain access to the child component instance, // you need to assign it to a `ref`, so we call `useRef()` to get one const childRef = useRef();
return ( <div> <Child ref={childRef} /> <button onClick={() => childRef.current.getAlert()}>Click</button> </div> ); };
ReactDOM.render( <Parent />, document.getElementById('root') );
<!-- end snippet -->

First off, let me express that this is generally _not_ the way to go about things in React land. Usually what you want to do is pass down functionality to children in props, and pass up notifications from children in events (or better yet: [`dispatch`](https://reactjs.org/docs/hooks-reference.html#usereducer)).
But if you _must_ expose an imperative method on a child component, you can use [refs](https://reactjs.org/docs/refs-and-the-dom.html). Remember this is an escape hatch and usually indicates a better design is available.
> Previously, refs were only supported for Class-based components. > With the advent of [React Hooks](https://reactjs.org/docs/hooks-intro.html), that's no longer the case
## Using Hooks and Function Components (`>= react@16.8`)
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
const { forwardRef, useRef, useImperativeHandle } = React;
// We need to wrap component in `forwardRef` in order to gain // access to the ref object that is assigned using the `ref` prop. // This ref is passed as the second parameter to the function component. const Child = forwardRef((props, ref) => {
// The component instance will be extended // with whatever you return from the callback passed // as the second argument useImperativeHandle(ref, () => ({
getAlert() { alert("getAlert from Child"); }
}));
return <h1>Hi</h1>; });
const Parent = () => { // In order to gain access to the child component instance, // you need to assign it to a `ref`, so we call `useRef()` to get one const childRef = useRef();
return ( <div> <Child ref={childRef} /> <button onClick={() => childRef.current.getAlert()}>Click</button> </div> ); };
ReactDOM.render( <Parent />, document.getElementById('root') );
<!-- language: lang-html -->
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<div id="root"></div>
<!-- end snippet -->
Documentation for `useImperativeHandle()` is [here](https://reactjs.org/docs/hooks-reference.html#useimperativehandle):
> `useImperativeHandle` customizes the instance value that is exposed to parent components when using `ref`.
## Using Class Components (`>= react@16.4`)
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
const { Component } = React;
class Parent extends Component { constructor(props) { super(props); this.child = React.createRef(); }
onClick = () => { this.child.current.getAlert(); };
render() { return ( <div> <Child ref={this.child} /> <button onClick={this.onClick}>Click</button> </div> ); } }
class Child extends Component { getAlert() { alert('getAlert from Child'); }
render() { return <h1>Hello</h1>; } }
ReactDOM.render(<Parent />, document.getElementById('root'));

<!-- language: lang-html -->
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script> <div id="root"></div>
<!-- end snippet -->
## Legacy API (`<= react@16.3`)
For historical purposes, here's the callback-based style you'd use with React versions before 16.3:

<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
const { Component } = React; const { render } = ReactDOM;
class Parent extends Component { render() { return ( <div> <Child ref={instance => { this.child = instance; }} /> <button onClick={() => { this.child.getAlert(); }}>Click</button> </div> ); } }
class Child extends Component { getAlert() { alert('clicked'); }
render() { return ( <h1>Hello</h1> ); } }

render( <Parent />, document.getElementById('app') );
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>
<!-- end snippet -->


        
Present in both answers; Present only in the new answer; Present only in the old answer;