简体   繁体   English

用子项方法转移道具中的成分

[英]Transfer component in props with children method

Its possible to transfer component in props with children method? 它可以通过子方法转移道具中的组件吗?

I have to components: 我要组件:

 let a = (<div>
        <button onClick={TContainer.METHOD}>TuCLIK</button>
  </div>);

 <TContainer data={ restaurantList } component={a}/>

I want to call method in childen but create element in parent. 我想在childen中调用方法,但在parent中创建元素。 I want to pass this component on props. 我想在道具上传递这个组件。

If its possible i dont know what writing in TContainer.METHOD to call childen method 如果可能的话,我不知道在TContainer.METHOD中写什么来调用childen方法

You are not passing a component in your props, it's an expression. 您没有在道具中传递组件 ,而是表达。
A component should be either a class the extends React.Component or a function that returns a jsx markup. 组件应该是扩展React.Componentclass ,或者是返回jsx标记的函数。
Now when we know that components are just functions, we know that we can pass parameters to them, hence we can pass a function reference as a parameter: 现在,当我们知道组件只是函数时,就知道可以将参数传递给它们,因此可以将函数引用作为参数传递:

let A = (onClick) => <div><button onClick={onClick}>TuCLIK</button></div>;
<TContainer data={ restaurantList } component={<A onClick={TContainer.METHOD} />}/>

Note that components should be capitalized. 请注意组件应大写。

Edit 编辑
As a followup to your comment, sorry but i misunderstood your question i guess. 作为对您的评论的跟进,很抱歉,我想我误解了您的问题。
You can't pass a reference of a method from a React component like that. 你不能像这样通过React组件传递方法的引用。

  • We can use couple of approaches regarding this scenario, one of them is to use this.props.children and pass the child component as a child. 对于这种情况,我们可以使用几种方法,其中一种方法是使用this.props.children并将child组件作为child传递。
    For example - <Parant><Child/></Parent> 例如- <Parant><Child/></Parent>
  • We can pass the child component as a prop. 我们可以将子组件作为道具传递。
    For example - <Parent component={Child} /> or <Parent component={<Child />} /> 例如- <Parent component={Child} /><Parent component={<Child />} />
  • We can write the parent component as a HOC and wrap the child with it. 我们可以将父组件编写为HOC,并用它包装子组件。
    For example - Parent(Child) 例如- Parent(Child)

In all the above examples you can't pass directly a reference of a function that is declared inside the parent's scope (as a prop to the child). 在上述所有示例中,您不能直接传递在父级作用域内声明的函数的引用(作为对子级的支持)。
In order to pass the child a prop within the parent's internal scope you should do it inside the render method. 为了在子级的内部范围内将子级传递给子项,您应该在render方法内执行子项。
For example: 例如:

  render() {
    return (
      <div>
        <this.props.component onClick={this.handleClick}/>
      </div>
    );
  }

This is a snippet that demonstrate one of the examples above: 这是一个片段,演示了上面的示例之一:

 const Child = ({onClick}) => <div onClick={onClick}>Im a child, Click me!</div> class Parent extends React.Component { constructor(props){ super(props); this.state = { counter: 0 } this.addCount = this.addCount.bind(this); } addCount(){ this.setState({counter: this.state.counter + 1}); } render() { return ( <div> <div>{`Count = ${this.state.counter}`}</div> <this.props.component onClick={this.addCount}/> </div> ); } } const App = () => <Parent component={Child} />; ReactDOM.render(<App />, document.getElementById('root')); 
 <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="root"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM