简体   繁体   English

React.js - 如何使用来自孩子的参数在父级中执行函数

[英]React.js - How to execute function in parent with arguments from child

The question is probably rather unclear, but i did not how to formulate it, maybe that was the reason why i was not able to find solution to this puzzle i have.这个问题可能相当不清楚,但我不知道如何制定它,也许这就是我无法找到解决这个难题的原因。 anyway, here is an example of what i want to accomplish:无论如何,这是我想要完成的一个例子:

<Calendar
      tileContent={({ activeStartDate, date, view }) =>
        this.renderGames(date, view)
      }
    />

This is an example from npm package react-calendar, but i am sure you know what i mean.这是 npm 包 react-calendar 的一个例子,但我相信你知道我的意思。 The param tileContent gets passed function that already has destructured object, and then i run my own function with data i get from that function.参数 tileContent 获得传递的函数,该函数已经具有解构对象,然后我使用从该函数获得的数据运行我自己的函数。

I was thinking that this was done by executing function in child where i would pass an object (or single param, i just use object as an example).我认为这是通过在 child 中执行函数来完成的,我将在其中传递一个对象(或单个参数,我只是以对象为例)。

It sounds like you want to execute a function that's in the parent component, from a child component with arguments passed from the child.听起来您想从子组件执行父组件中的函数,并从子组件传递参数。

Here is an example:下面是一个例子:

    const ParentComponent = () => {

      const handleClick = (args) => {
         console.log(args)
      }


      return (
         <div>
            <ChildComponent onClick={handleClick} />
         </div>

      )

}

const ChildComponent = ({onClick}) => {
   const val = 5;


   return (
      <div>
         <button onClick={() => handleClick(val)} name="Click">Click Me</button>
      </div>
   )
}

This hsould render the child component which is just a button, with an event handler that is sent from the parent.这应该渲染子组件,它只是一个按钮,带有从父级发送的事件处理程序。 When you click the button, you should get a console log of 5, which is coming from the parent.当您单击该按钮时,您应该会获得 5 的控制台日志,该日志来自父级。 This is how you would propgate values from the child, up to the parent.这就是您将值从子级传播到父级的方式。

I think what you're looking for are Render Props , not just executing function in parent with args (even though render props do this as well).我认为您正在寻找的是Render Props ,而不仅仅是使用 args 在父级中执行函数(即使渲染道具也可以这样做)。 It would appear your example is using Render Props specifically.看起来您的示例专门使用了渲染道具。

There are some good examples online of using render props in React, also referred to as "Wrapper Components", etc..网上有一些在 React 中使用渲染道具的好例子,也被称为“包装组件”等。

An example could be something like:一个例子可能是这样的:

 const { render } = ReactDOM; class CounterWrapper extends React.Component { state = { count: 0 }; increment = () => { const { count } = this.state; return this.setState({ count: count + 1 }); }; decrement = () => { const { count } = this.state; return this.setState({ count: count - 1 }); }; render() { const { count } = this.state; return ( <React.Fragment> {this.props.wrapperContent({ increment: this.increment, decrement: this.decrement, count })} </React.Fragment> ); } } class App extends React.Component { renderApp = (cnt, inc, dec) => { return ( <div> <h1>Render Props Counter Example</h1> <div> <p>{cnt}</p> <button type="button" onClick={() => inc()}> Increment </button> <button type="button" onClick={() => dec()}> Decrement </button> </div> </div> ) }; render() { return ( <CounterWrapper wrapperContent={({ count, increment, decrement }) => this.renderApp(count, increment, decrement) } /> ); } } render(<App />, document.body);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

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

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