1//Form (Parent)
2import React, { useState, Component } from 'react';
3import Input from '../../shared/input-box/InputBox'
4
5const Form = function (props) {
6
7 const [value, setValue] = useState('');
8
9 const onchange = (data) => {
10 setValue(data)
11 console.log("Form>", data);
12 }
13
14 return (
15 <div>
16 <Input data={value} onchange={(e) => { onchange(e) }}/>
17 </div>
18 );
19}
20export default Form;
21
22//Input Box (Child)
23import React from 'react';
24
25const Input = function (props) {
26 console.log("Props in Input :", props);
27
28 const handleChange = event => {
29 props.onchange(event.target.value);
30 }
31
32 return (
33 <div>
34 <input placeholder="name"
35 id="name"
36 onChange= {handleChange}
37 />
38 </div>
39 );
40}
41export default Input;
1import React, { useState } from 'react';
2import './App.css';
3import Todo from './components/Todo'
4
5
6
7function App() {
8 const [todos, setTodos] = useState([
9 {
10 id: 1,
11 title: 'This is first list'
12 },
13 {
14 id: 2,
15 title: 'This is second list'
16 },
17 {
18 id: 3,
19 title: 'This is third list'
20 },
21 ]);
22
23return (
24 <div className="App">
25 <h1></h1>
26 <Todo todos={todos}/> //This is how i'm passing props in parent component
27 </div>
28 );
29}
30
31export default App;
32
33
34function Todo(props) {
35 return (
36 <div>
37 {props.todos.map(todo => { // using props in child component and looping
38 return (
39 <h1>{todo.title}</h1>
40 )
41 })}
42 </div>
43 );
44}