|
1 | | -import React, { Component } from 'react'; |
2 | | -import logo from './logo.svg'; |
3 | | -import './App.css'; |
| 1 | +import React, { useState } from 'react'; |
4 | 2 |
|
| 3 | +// component manages itself without external API |
| 4 | +// not useful for real applications, |
| 5 | +// but just for the sake of demonstrating uncontrolled/controlled components |
| 6 | + |
| 7 | +import StandaloneTextInput from './Components/StandaloneTextInput'; |
| 8 | + |
| 9 | +// components managed from outside (e.g. App component) |
| 10 | + |
| 11 | +import TextInput from './Components/TextInput'; |
5 | 12 | import Select from './Components/Select'; |
6 | 13 | import NumberInput from './Components/NumberInput'; |
7 | | -import TextInput from './Components/TextInput'; |
8 | 14 | import Checkbox from './Components/Checkbox'; |
9 | 15 | import Radio from './Components/Radio'; |
10 | 16 |
|
11 | | -class App extends Component { |
12 | | - constructor(props) { |
13 | | - super(props); |
14 | | - |
15 | | - this.state = { |
16 | | - selectValue: 'grapefruit', |
17 | | - numberInputValue: 0, |
18 | | - textInputValue: 'Hello World', |
19 | | - checkboxValue: true, |
20 | | - radioValue: 'small', |
21 | | - }; |
22 | | - |
23 | | - this.handleSelectChange = this.handleSelectChange.bind(this); |
24 | | - this.handleNumberInputChange = this.handleNumberInputChange.bind(this); |
25 | | - this.handleTextInputChange = this.handleTextInputChange.bind(this); |
26 | | - this.handleCheckboxChange = this.handleCheckboxChange.bind(this); |
27 | | - this.handleRadioChange = this.handleRadioChange.bind(this); |
28 | | - } |
29 | | - |
30 | | - handleSelectChange(value) { |
31 | | - this.setState({ selectValue: value }); |
32 | | - } |
33 | | - |
34 | | - handleNumberInputChange(value) { |
35 | | - this.setState({ numberInputValue: value }); |
36 | | - } |
37 | | - |
38 | | - handleTextInputChange(value) { |
39 | | - this.setState({ textInputValue: value }); |
40 | | - } |
41 | | - |
42 | | - handleCheckboxChange(value) { |
43 | | - this.setState({ checkboxValue: value }); |
44 | | - } |
45 | | - |
46 | | - handleRadioChange(value) { |
47 | | - this.setState({ radioValue: value }); |
48 | | - } |
49 | | - |
50 | | - render() { |
51 | | - const { |
52 | | - selectValue, |
53 | | - numberInputValue, |
54 | | - textInputValue, |
55 | | - checkboxValue, |
56 | | - radioValue, |
57 | | - } = this.state; |
58 | | - |
59 | | - return ( |
60 | | - <div className="App"> |
61 | | - <header className="App-header"> |
62 | | - <img src={logo} className="App-logo" alt="logo" /> |
63 | | - <h1 className="App-title">Welcome to React</h1> |
64 | | - </header> |
65 | | - |
66 | | - <p className="App-intro"> |
67 | | - To get started, edit <code>src/App.js</code> and save to reload. |
| 17 | +const App = () => { |
| 18 | + const [textInputValue, setRextInputValue] = useState('Hello World'); |
| 19 | + const [selectValue, setSelectValue] = useState('grapefruit'); |
| 20 | + const [numberInputValue, setNumberInputValue] = useState(0); |
| 21 | + const [checkboxValue, setCheckboxValue] = useState(true); |
| 22 | + const [radioValue, setRadioValue] = useState('small'); |
| 23 | + |
| 24 | + const handleTextInputChange = value => { |
| 25 | + setRextInputValue(value); |
| 26 | + }; |
| 27 | + |
| 28 | + const handleSelectChange = value => { |
| 29 | + setSelectValue(value); |
| 30 | + }; |
| 31 | + |
| 32 | + const handleNumberInputChange = value => { |
| 33 | + setNumberInputValue(value); |
| 34 | + }; |
| 35 | + |
| 36 | + const handleCheckboxChange = value => { |
| 37 | + setCheckboxValue(value); |
| 38 | + }; |
| 39 | + |
| 40 | + const handleRadioChange = value => { |
| 41 | + setRadioValue(value); |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <div> |
| 46 | + {/* Uncontrolled and Controlled Input Example, State/Functions managed within Component */} |
| 47 | + <div> |
| 48 | + <StandaloneTextInput /> |
| 49 | + </div> |
| 50 | + |
| 51 | + <hr /> |
| 52 | + |
| 53 | + {/* Different Controlled Inputs, State/Callbacks managed in App Components */} |
| 54 | + <div> |
| 55 | + <TextInput |
| 56 | + value={textInputValue} |
| 57 | + onTextInputChange={handleTextInputChange} |
| 58 | + > |
| 59 | + Type a text |
| 60 | + </TextInput> |
| 61 | + <p> |
| 62 | + <strong>Result:</strong> {textInputValue} |
68 | 63 | </p> |
| 64 | + </div> |
69 | 65 |
|
70 | | - <div> |
71 | | - <Select value={selectValue} onSelectChange={this.handleSelectChange}>Choose a fruit</Select> |
72 | | - Result: {selectValue} |
73 | | - </div> |
74 | | - <div> |
75 | | - <NumberInput value={numberInputValue} onNumberInputChange={this.handleNumberInputChange} min={-1} max={3}>Choose a number</NumberInput> |
76 | | - Result: {numberInputValue} |
77 | | - </div> |
78 | | - <div> |
79 | | - <TextInput value={textInputValue} onTextInputChange={this.handleTextInputChange}>Type a text</TextInput> |
80 | | - Result: {textInputValue} |
81 | | - </div> |
82 | | - <div> |
83 | | - <Checkbox value={checkboxValue} onCheckboxChange={this.handleCheckboxChange}>Check the box</Checkbox> |
84 | | - Result: {checkboxValue.toString()} |
85 | | - </div> |
86 | | - <div> |
87 | | - <Radio value={radioValue} onRadioChange={this.handleRadioChange}>Choose a size</Radio> |
88 | | - Result: {radioValue} |
89 | | - </div> |
| 66 | + <hr /> |
| 67 | + |
| 68 | + <div> |
| 69 | + <Select |
| 70 | + value={selectValue} |
| 71 | + onSelectChange={handleSelectChange} |
| 72 | + > |
| 73 | + Choose a fruit |
| 74 | + </Select> |
| 75 | + <p> |
| 76 | + <strong>Result:</strong> {selectValue} |
| 77 | + </p> |
| 78 | + </div> |
| 79 | + |
| 80 | + <hr /> |
| 81 | + |
| 82 | + <div> |
| 83 | + <NumberInput |
| 84 | + value={numberInputValue} |
| 85 | + onNumberInputChange={handleNumberInputChange} |
| 86 | + min={-1} |
| 87 | + max={3} |
| 88 | + > |
| 89 | + Choose a number |
| 90 | + </NumberInput> |
| 91 | + <p> |
| 92 | + <strong>Result:</strong> {numberInputValue} |
| 93 | + </p> |
| 94 | + </div> |
| 95 | + |
| 96 | + <hr /> |
| 97 | + |
| 98 | + <div> |
| 99 | + <Checkbox |
| 100 | + value={checkboxValue} |
| 101 | + onCheckboxChange={handleCheckboxChange} |
| 102 | + > |
| 103 | + Check the box |
| 104 | + </Checkbox> |
| 105 | + <p> |
| 106 | + <strong>Result:</strong> {checkboxValue.toString()} |
| 107 | + </p> |
| 108 | + </div> |
| 109 | + |
| 110 | + <hr /> |
| 111 | + |
| 112 | + <div> |
| 113 | + <Radio value={radioValue} onRadioChange={handleRadioChange}> |
| 114 | + Choose a size |
| 115 | + </Radio> |
| 116 | + <p> |
| 117 | + <strong>Result:</strong> {radioValue} |
| 118 | + </p> |
90 | 119 | </div> |
91 | | - ); |
92 | | - } |
93 | | -} |
| 120 | + </div> |
| 121 | + ); |
| 122 | +}; |
94 | 123 |
|
95 | 124 | export default App; |
0 commit comments