Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React expose component function

Based on the example on this link http://reactjs.cn/react/tips/expose-component-functions.html, I have been trying to simplify the code to understand exposed methods better, so I got the following, which doesn't work, the error is "Uncaught TypeError: Cannot read property 'animate' of undefined" and I don't really know the reason:

var Todo = React.createClass({
    render: function() {
        return <div></div>;
    },

    //this component will be accessed by the parent through the `ref` attribute
    animate: function() {
        console.log('Pretend  is animating');
    }
});


var Todos = React.createClass({

    render: function() {
        return (
                <div>
                    <Todo ref='hello' />
                    {this.refs.hello.animate()}
                </div>
        );
    }
});

ReactDOM.render(<Todos />, app);
like image 319
MariaZ Avatar asked Jan 04 '17 23:01

MariaZ


People also ask

How do you call a function from one component to another in react JS?

Second Component class PopupOver extends React. Component{ constructor(){ super(); // here i need to call Header class function check click.... // How to call Header. checkClick() from this class } render(){ return ( <div className="displayinline col-md-12 "> Hello </div> ); } } export default PopupOver; reactjs.

How do you pass ref to the functional component?

To create a ref in a functional component we use the useRef() hook which returns a mutable object with a . current property set to the initialValue we passed to the hook. This returned object will persist for the full lifetime of the component. Thus, throughout all of its re-rendering and until it unmounts.

Can we use REF in functional component?

When the ref attribute is used on a custom class component, the ref object receives the mounted instance of the component as its current . You may not use the ref attribute on function components because they don't have instances.

What is createRef in React?

This method is used to access any DOM element in a component and it returns a mutable ref object which will be persisted as long as the component is placed in the DOM. If we pass a ref object to any DOM element, then the. current property to the corresponding DOM node elements will be added whenever the node changes.


1 Answers

you don't have the reference to the element in the first render, because it isn't mounted it.

you can do something like this to make it work:

var Todos = React.createClass({
    componentDidMount: function() {
        this.refs.hello.animate();
    },
    render: function() {
        return (
                <div>
                    <Todo ref='hello' />
                </div>
        );
    }
});

componentDidMount is called when the component is already been rendered (for the first time). here you will have the reference to the element

like image 166
Doron Brikman Avatar answered Oct 14 '22 19:10

Doron Brikman