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;
1const { useState } = React;
2
3function PageComponent() {
4 const [count, setCount] = useState(0);
5 const increment = () => {
6 setCount(count + 1)
7 }
8
9 return (
10 <div className="App">
11 <ChildComponent onClick={increment} count={count} />
12 <h2>count {count}</h2>
13 (count should be updated from child)
14 </div>
15 );
16}
17
18const ChildComponent = ({ onClick, count }) => {
19 return (
20 <button onClick={onClick}>
21 Click me {count}
22 </button>
23 )
24};
25
26ReactDOM.render(<PageComponent />, document.getElementById("root"));
1Parent:
2
3<div className="col-sm-9">
4 <SelectLanguage onSelectLanguage={this.handleLanguage} />
5</div>
6
7Child:
8
9handleLangChange = () => {
10 var lang = this.dropdown.value;
11 this.props.onSelectLanguage(lang);
12}
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}