[英]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.