How to pass a touch event via props to a child component?

Asked

Viewed 700 times

0

I have a parent component of which I have a button and I want to pass onPress to the child component, I am new to React-Native and I have this doubt, you can help me ??

  • put your code, will help

  • Welcome, I recommend you take a [tour] and see [mcve].

  • I put an answer, but, missing in your question the code, basically the event can be shared in other components

2 answers

0

Basically that’s how:

class Pai extends React.Component { 
  onEventClick() {
    console.log('clicado');
  }
  render() {
    return (
      <div>
        <h3>Pai</h3>        
        <Filho onEventPropsClick={this.onEventClick.bind(this)} />
      </div>
    );
  }
}

class Filho extends React.Component {
  constructor(props){
    super(props);    
  }
  render() {
    const { onEventPropsClick } = this.props;
    return (
      <div>
        <h4>Filho</h4>
        <button onClick={() => onEventPropsClick()}>
          Clique Aqui
       </button>
      </div>
    );
  }
}

ReactDOM.render(<Pai/>,document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root">Carregando ...</div>

  • That’s right, gave to understand his example despite being using React-Native. Thanks

  • is the same thing @Hervessonporto

  • OK, thanks for the help

0

Fala Hervesson.

You didn’t put the code, but I imagine it’s something like this

PARENT COMPONENT

import React, {Component} from 'react';
import ComponenteFILHO from './ComponenteFILHO';
...

export default class ComponentePAI extends Component {
  state = {
    parametro_para_filho: "",
  };

  handleOnPress = () => {
    // Faz alguma coisa legal ou não.
    this.setState({ parametro_para_filho: "uma coisa legal" })
  };

  render() {
    return(
      <View>
      <TouchableOpacity onPress={() => this.handleOnPress()}>
        <Text>Aperte aqui</Text>
      </TouchableOpacity>
      <ComponenteFILHO handleOnPress={() => this.handleOnPress()} dePaiPraFilho={this.state.parametro_para_Filho} />
     </View>
    );
  }
}

In the child component that function or parameter comes via Props comes via props.

Component Filho

import React from 'react';
import ...

const ComponenteFILHO = props => ( // pode usar desestruturação.
  <View>
    <TouchableOpacity onPress={() => props.handleOnPress()}>
      <Text>Clique aqui e veja o texto abaixo mudando</Text>
    </TouchableOpacity>
    <Text>{props.dePaiPraFilho}</Text>
  </View>
);

I did not do any styling there, but it is necessary for you to see the final result. Send everything to the middle.

  • Here the function being fired is in the Parent Component, in case I want the touch event to be given in the Parent component and the function to be fired in the Child Component.

  • but somehow I managed to solve this problem

  • Okay then! Glad you made it. Up until!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.