简体   繁体   English

组件按钮调用父级中的函数

[英]Component button call a function in parent

I have this Parent Component:我有这个父组件:

import React from "react";
import ReactDOM from "react-dom";
import "./App.css";
import Instructions from "./components/instructions/instructions";
import Generate from "./components/button/button";
import View from "./components/view/view";

export class Generator extends React.Component {
    constructor () {
        super()
        this.state = { 
        onView: '0' 
      }  
    }
      btnClick() {
      var x = Math.ceil(Math.ceil(Math.random()) / Math.random());
      console.log(x);
     }

  render() {

  return (
    <div className="container">
    <Instructions />
    <Generate currentClick={this.btnClick}/>
    <View show={this.state.onView}/>
    </div>
    );
  }
}

export default Generator;

and I have this child component:我有这个子组件:

import React from "react";

class Generate extends React.Component {
    constructor(props) {
        super(props)
        }

        handleClick = () => {
            this.props.btnClick();
        }

    render() {

    return (
        <div className="gen-box">
        <button 
        type="button" 
        className="gen-btn" 
        onClick={this.handleClick}>
        generate 
        </button>
        </div>
        );
    }
}

export default Generate;

Now, I want that every click in the button component, to activate a function in the parent component to generate a random number.现在,我希望每次单击按钮组件时,都会激活父组件中的一个函数以生成一个随机数。 My code here did not work like I expected, were did I go wrong?我这里的代码没有像我预期的那样工作,是我出错了吗?

You are exposing the function with prop name currentClick .您正在使用道具名称currentClick公开该函数。 It's like you are assigning function to currentClick .就像您将函数分配给currentClick

You can use like this.你可以这样使用。

import React from "react";
class Generate extends React.Component {
  constructor(props) {
    super(props)
  }
  handleClick = () => {
    this.props.currentClick();
  }
  render() {
    return (
      <div className="gen-box">
        <button
          type="button"
          className="gen-btn"
          onClick={this.handleClick}>
          generate
        </button>
      </div>
    );
  }
}
export default Generate;

Or directly call the function in onclick.或者直接在onclick中调用该函数。

import React from "react";
class Generate extends React.Component {
  constructor(props) {
    super(props)
  }

  render() {
    return (
      <div className="gen-box">
        <button
          type="button"
          className="gen-btn"
          onClick={this.props.currentClick}>
          generate
        </button>
      </div>
    );
  }
}
export default Generate;

You are calling a wrong function, call this.props.currentClick();你调用了一个错误的函数,调用this.props.currentClick(); from child.从孩子。 And Make Parent btnClick function as narrow funtion or bind it, and then set that random value to state, after that you can see random number each time you click child button.并将父 btnClick 函数设为窄函数或绑定它,然后将该随机值设置为 state,之后您每次单击子按钮时都可以看到随机数。

Your child component onClick function suppose to be like this.您的子组件 onClick 函数假设是这样的。

  handleClick = () => {
        this.props.currentClick();
    }

from parent your sending props as从父母你发送道具作为

 <Generate currentClick={this.btnClick}/>

where currentClcik is the props suppose to be called in the child component and not btnClick其中currentClcik是假设在子组件中调用的道具,而不是btnClick

you have to use a callback function as prop for the children component, like this:您必须使用callback函数作为子组件的prop ,如下所示:

Generator:发电机:

import React from "react";
import ReactDOM from "react-dom";
import "./App.css";
import Instructions from "./components/instructions/instructions";
import Generate from "./components/button/button";
import View from "./components/view/view";

export class Generator extends React.Component {
    constructor () {
        super()
        this.state = { 
          onView: '0' 
        }  
    }

    btnClick() {
      var x = Math.ceil(Math.ceil(Math.random()) / Math.random());
      console.log(x);
    }

  render() {
  return (
    <div className="container">
    <Instructions />
    <Generate currentClick={this.btnClick}/>
    <View show={this.state.onView}/>
    </div>
    );
  }
}

export default Generator;

Generate:产生:

import React from "react";

class Generate extends React.Component {
    constructor(props) {
        super(props)
        }

        handleClick = () => {
            this.props.currentClick();
        }

    render() {

    return (
        <div className="gen-box">
        <button 
        type="button" 
        className="gen-btn" 
        onClick={this.handleClick}>
        generate 
        </button>
        </div>
        );
    }
}

export default Generate;

You have to use the same name as the prop that you're passing to the Children Generate component.您必须使用与传递给 Children Generate组件的prop相同的名称。 Hope this helps.希望这可以帮助。

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

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