react get props from parent code example

Example 1: react pass prop to parent

//Form (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 (
        
{ onchange(e) }}/>
); } export default Form; //Input Box (Child) import React from 'react'; const Input = function (props) { console.log("Props in Input :", props); const handleChange = event => { props.onchange(event.target.value); } return (
); } export default Input;

Example 2: pass props in react

/* PASSING THE PROPS to the 'Greeting' component */
const expression = 'Happy';
 // statement and expression are the props (ie. arguments) we are passing to Greeting component

/* USING THE PROPS in the child component */
class Greeting extends Component {
  render() {
    return 

{this.props.statement} I am feeling {this.props.expression} today!

; } }

Tags:

Misc Example