简体   繁体   English

如何在反应中将组件传递给 onClick

[英]How to pass component to onClick in react

import React from 'react'

export default () => {
  function clickHandler() {
    console.log('Button clicked')
  }
  return (
    <div>
      <button onClick={clickHandler}>Click</button>
    </div>
  )
}

In the above code we see that a function has been passed to the onClick.In the same way to the onClick I need to pass a diffrent component which is present in the same src.在上面的代码中,我们看到 function 已传递给 onClick。以与 onClick 相同的方式,我需要传递存在的相同组件。 This component consists of a.js and a.css file.Could you please help me out with it.该组件由 a.js 和 a.css 文件组成。请您帮我解决一下。 Thanks in advance提前致谢

If you don't mind using classes instead of functions, your other component should look like this:如果您不介意使用类而不是函数,那么您的其他组件应如下所示:

import React from 'react'

class ShowThisAfterClick extends React.Component {
  return (
    <div>
      <p>This is what you want to show</p>
    </div>
  )
}
export default ShowThisAfterClick

And now you should update the component you've shown:现在您应该更新您显示的组件:

import React from 'react'
import ShowThisAfterClick from './where/you/put/the/ShowThisAfterClick.js'

class Main extends React.Component {
  constructor(props){
    super(props)
    this.state = { isButtonClicked: false }

    this.clickHandler = this.clickhandler.bind(this)
  }
  
  clickHandler() {
    this.setState({ isButtonClicked: true })
  }
  
  render() {
    const { isButtonClicked } = this.state
    return (
      <div>
        <button onClick={ this.clickHandler }>Click</button>
        { isButtonClicked ? <ShowThisAfterClick /> : ''}
      </div>
    )
  }
}
export default Main

If you want to keep using functions, then I would kindly suggest to read the manual , it is more than well written.如果您想继续使用功能,那么我建议您阅读手册,它写得非常好。

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

相关问题 如何将“onClick”function 传递给 React 中的子组件 - How to pass the 'onClick' function to child component in React 如何在 React 功能组件中的 onClick 上传递参数 - How to pass parameter on onClick in React Functional Component 如何在 React JS 中将 props onClick 从一个组件传递到另一个组件 - How to pass props onClick from one component to another in React JS 如何在 React 中将 onClick function 传递给孙子组件? - How do you pass an onClick function to grandchild component in React? 将onClick事件传递给React中的Button组件 - Pass onClick event to Button Component in React 我如何通过onclick冷组件将数据传递给父组件做出反应 - How can i pass data by onclick chilld component to parent component react 如何将 onClick() 事件从 React 中父组件中的按钮传递给子组件? - How to pass the onClick() event to a child component from a button in the parent component in React? 如何使用 onClick 从 JSX 组件向反应函数组件传递参数 - How to pass arguments to a react function component from a JSX component using onClick 如何在 React.js 中使用 onClick() 事件将道具从一个组件传递到另一个组件 - How to pass props from one Component to another component with onClick() event in React.js React:将函数作为道具传递给子组件,在子组件中调用onClick - React: Pass function to child component as props, call onClick in child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM