Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output events's Angular in ReactJs

I was looking the way to create in ReactJs similar output events like Angular.

I am making a library of components in ReactJs according to Atomic design, so, I have, for example, a Button injected in other component, and I would like to know how I could write a props for Button, so everytime the user press the button would launch this prop(a output in Angular way) to be detected in the parent where it was injected.

I don't want to use the State because I want that the components are fully independent.

Thanks in advance

like image 416
Agustin Herrera Avatar asked Jan 20 '18 14:01

Agustin Herrera


1 Answers

There is no equivalent to @output EventEmitterin react. The standard way to do this is to pass a callback in the props to the child component. Like so:

  class Parent extends React.Component {
      handleChildClicked = () => {
          console.log('child clicked');
      }
      render() {
        <Child onClick={this.handleChildClicked} />
      }
  }

  const Child = (props: Props) => {
    const { onClick } = props;
    return (
      <button onClick={onClick}>Clicky!</button>
    );
  };
like image 176
dashton Avatar answered Oct 26 '22 23:10

dashton