how to pass different commpomnnent to parent in react code example
Example 1: pass data from child component to parent component react native
class Parent extends React.Component {
state = { message: "" }
callbackFunction = (childData) => {
this.setState({message: childData})
},
render() {
return (
<div>
<Child1 parentCallback = {this.callbackFunction}/>
<p> {this.state.message} </p>
</div>
);
}
}
class Child1 extends React.Component{
sendData = () => {
this.props.parentCallback("Hey Popsie, How’s it going?");
},
render() {
}
};
Example 2: react pass prop to parent
import React, { useState, Component } from 'react';
import Input from '../../shared/input-box/InputBox'
const Form = function (props) {
const [value, setValue] = useState('');
const onchange = (data) => {
setValue(data)
console.log("Form>", data);
}
return (
<div>
<Input data={value} onchange={(e) => { onchange(e) }}/>
</div>
);
}
export default Form;
import React from 'react';
const Input = function (props) {
console.log("Props in Input :", props);
const handleChange = event => {
props.onchange(event.target.value);
}
return (
<div>
<input placeholder="name"
id="name"
onChange= {handleChange}
/>
</div>
);
}
export default Input;