How to pass props to {this.props.children}

后端 未结 26 3420
猫巷女王i
猫巷女王i 2020-11-21 23:42

I\'m trying to find the proper way to define some components which could be used in a generic way:


  
  

        
相关标签:
26条回答
  • 2020-11-22 00:13

    Cleaner way considering one or more children

    <div>
       { React.Children.map(this.props.children, child => React.cloneElement(child, {...this.props}))}
    </div>
    
    0 讨论(0)
  • 2020-11-22 00:13

    Some reason React.children was not working for me. This is what worked for me.

    I wanted to just add a class to the child. similar to changing a prop

     var newChildren = this.props.children.map((child) => {
     const className = "MenuTooltip-item " + child.props.className;
        return React.cloneElement(child, { className });
     });
    
     return <div>{newChildren}</div>;
    

    The trick here is the React.cloneElement. You can pass any prop in a similar manner

    0 讨论(0)
  • 2020-11-22 00:14

    Is this what you required?

    var Parent = React.createClass({
      doSomething: function(value) {
      }
      render: function() {
        return  <div>
                  <Child doSome={this.doSomething} />
                </div>
      }
    })
    
    var Child = React.createClass({
      onClick:function() {
        this.props.doSome(value); // doSomething is undefined
      },  
      render: function() {
        return  <div onClick={this.onClick}></div>
      }
    })
    
    0 讨论(0)
  • 2020-11-22 00:15

    If you have multiple children you want to pass props to, you can do it this way, using the React.Children.map:

    render() {
        let updatedChildren = React.Children.map(this.props.children,
            (child) => {
                return React.cloneElement(child, { newProp: newProp });
            });
    
        return (
            <div>
                { updatedChildren }
            </div>
        );
    }
    

    If your component is having just one child, there's no need for mapping, you can just cloneElement straight away:

    render() {
        return (
            <div>
                {
                    React.cloneElement(this.props.children, {
                        newProp: newProp
                    })
                }
            </div>
        );
    }
    
    0 讨论(0)
  • 2020-11-22 00:15

    Got inspired by all the answers above and this is what I have done. I am passing some props like some data, and some components.

    import React from "react";
    
    const Parent = ({ children }) => {
      const { setCheckoutData } = actions.shop;
      const { Input, FieldError } = libraries.theme.components.forms;
    
      const onSubmit = (data) => {
        setCheckoutData(data);
      };
    
      const childrenWithProps = React.Children.map(
        children,
        (child) =>
          React.cloneElement(child, {
            Input: Input,
            FieldError: FieldError,
            onSubmit: onSubmit,
          })
      );
    
      return <>{childrenWithProps}</>;
    };
    
    
    0 讨论(0)
  • 2020-11-22 00:18

    You no longer need {this.props.children}. Now you can wrap your child component using render in Route and pass your props as usual:

    <BrowserRouter>
      <div>
        <ul>
          <li><Link to="/">Home</Link></li>
          <li><Link to="/posts">Posts</Link></li>
          <li><Link to="/about">About</Link></li>
        </ul>
    
        <hr/>
    
        <Route path="/" exact component={Home} />
        <Route path="/posts" render={() => (
          <Posts
            value1={1}
            value2={2}
            data={this.state.data}
          />
        )} />
        <Route path="/about" component={About} />
      </div>
    </BrowserRouter>
    
    0 讨论(0)
提交回复
热议问题