[英]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.Component
的class
,或者是返回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组件传递方法的引用。
this.props.children
and pass the child component as a child. this.props.children
并将child组件作为child传递。 <Parant><Child/></Parent>
<Parant><Child/></Parent>
<Parent component={Child} />
or <Parent component={<Child />} />
<Parent component={Child} />
或<Parent component={<Child />} />
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.