Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJs. Get children component

I've component wrapper for Bootstrap Panel:

var Panel = React.createClass({
    render: function () {
        return (
            <div className="panel panel-default">
                <div className="panel-heading">
                    <div className="panel-title">
                        {this.props.title}
                    </div>
                </div>
                <div className="panel-body"></div>
            </div>
        );
    }
});

How to output to "panel-body" tag "h1" and component "AvailableActions" on example what you can see below?

var PlayerActions = React.createClass({
    render: function () {
        return (
            <Panel title="Actions">
                <h1>Some description here...</h1>
                <AvailableActions></AvailableActions>
            </Panel>
        );
    }
});
like image 788
Alexandr Avatar asked Feb 20 '16 21:02

Alexandr


People also ask

How do you access the children of component React?

In React we can access the child's state using Refs. we will assign a Refs for the child component in the parent component. then using Refs we can access the child's state. Creating Refs Refs are created using React.

How do I get child props from components?

To pass data from a child component to its parent, we can call a parent function from the child component with arguments. The parent function can be passed down to the child as a prop, and the function arguments are the data that the parent will receive.

How do I get onClick to child component?

You can also pass events like onClick or OnChange Just call an alert method in the childToParent function and pass that function as a prop to the child component. And in the child component, accept the childToParent function as a prop. Then assign it to an onClick event on a button. That's it!


1 Answers

Seems you need this.props.children

var Panel = React.createClass({
    render: function () {
        return (
            <div className="panel panel-default">
                <div className="panel-heading">
                    <div className="panel-title">
                        {this.props.title}
                    </div>
                </div>
                <div className="panel-body">{ this.props.children }</div>
            </div>
        );
    }
});

Example

like image 91
Oleksandr T. Avatar answered Oct 25 '22 02:10

Oleksandr T.