简体   繁体   English

如何从另一个组件的onclick事件触发功能

[英]How to trigger a function from another component's onclick event

So I'm new in ReactJS and there's a cool company offering a junior react developer position if I solve their test project correctly. 所以我是ReactJS的新手,如果我正确地解决了他们的测试项目,那么有一家很酷的公司会提供一个初级的React开发人员职位。 In the project, I have to draw four svg components and handle some simple events with only one user interaction which is a button click event. 在项目中,我必须绘制四个svg组件并仅通过一个用户交互(即按钮单击事件)来处理一些简单事件。 So everything seems quite simple but the thing is, I'm stucked in the middle of the project. 因此,一切似乎都非常简单,但事实是,我陷入了项目的中间。

I want to give a shake animation to one of the components with a click event in the button component. 我想通过按钮组件中的click事件为其中一个组件提供摇动动画。 When Button is clicked, the Tree should shake for 3 seconds and one other action will be triggered after that. 单击“按钮”时,树应摇动3秒钟,之后将触发另一动作。 I prepared a css animation and I want to change the class of the Tree component every time when the Button component clicked. 我准备了一个CSS动画,并且每次单击Button组件时都想更改Tree组件的类。 Could not find out how to do so. 无法找到具体方法。

So here is my button component 这是我的按钮组件

class ShakeButton extends React.Component {
    this.props.onClick(this.props.activeClass==='shake')

    render() {
        return (
            <g id="shakeButton" transform="scale(0.15,0.15), translate(3000,-1000)"  onClick={this.handleClick}>
                //a good amount of svg content here
            </g>
        )
    }
}

export default ShakeButton

and here my Tree component (which is gonna change its class name for css animation) 这是我的Tree组件(它将更改其CSS动画的类名)

import React, { Components } from "react";
import  "./cssTree.css";

class AppleTree extends React.Component{
//   state = {
//     activeClass: ''
//   } n
// handleClick = () => this.props.onClick(this.props.activeClass==='shake')

  render() {
    return (
      <g className="" id="appleTree" >
        <g transform="translate(-1000 -1300) scale(1.5)">
         //again, some crowded svg content here
        </g>
      </g>
    )
  }
}

export default AppleTree

I collect all of my components in a component named <Canvas /> and render it inside the App.js 我将所有组件收集在名为<Canvas />的组件中,并将其呈现在App.js中

You could try lift the onClick method and state up , meaning get it in your parent component ( App I suppose), then pass it down to child components. 您可以尝试提升onClick方法并进行状态设置 ,这意味着将其放在父组件中(我想是App ),然后将其传递给子组件。

class App extends React.Component{  
  constructor(props) {
    super(props);
    this.state({
      className: ''
    });
  }
  handleClick = () => {
    this.setState({ className: 'shake' });
  }

  render (
    return (
    // all your components
    <Button clickHandler={this.handleClick} />
    <AppleTree class={this.state.className} />
    )
  )
}

// Button component: Stateless if it doesn't need it's own state
const Button = ({ clickHandler }) => {
 return (
   <g id="shakeButton" transform="scale(0.15,0.15), translate(3000,-1000)"  onClick={() => clickHandler()}>
   //a good amount of svg content here
   </g>
 )
}

// Stateless if it doesn't need its own state
const AppleTree = ({ class }) => {
    return (
      <g className={class} id="appleTree" >
        <g transform="translate(-1000 -1300) scale(1.5)">
         //again, some crowded svg content here
        </g>
      </g>
    )
}


暂无
暂无

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

相关问题 React:如何在“onClick”事件上触发一个函数,但在一个组件上? - React : How to trigger a function on the "onClick" event, but on a component? 安装组件时onClick的功能触发器 - onClick's function trigger when component is mounted 如何在组件内部也使用onClick事件仅触发一个onClick函数? - How to only trigger one onClick function when inside a component also with onClick event? 如何测试由调用 setState function 的 onClick 事件所做的更改,该事件从另一个组件传递并更改 UI? - How to test changes made by onClick event that calls a setState function, which is passed from another component and changes UI? 使用 onclick 事件触发 function - Trigger function with onclick event 如何使用 React Hooks 从子组件 onClick() 触发父组件中的事件? - How to trigger an event in Parent Component from Child Component onClick() using React Hooks? 如何从 React 中的另一个兄弟组件触发兄弟组件 function? - How to trigger sibling component function from another sibling component in React? 如何从ReactJS中的onClick函数调用另一个组件 - How to call another component from onClick function in ReactJS 如何通过点击可触摸组件来触发另一个可触摸组件的按下事件? - How to trigger another Touchable component's press event by tap a touchable component? 如何在本机反应中从另一个组件触发功能? - How to trigger a function from another component in react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM