The Complete React Web Developer Course (2nd Edition)


05 Stateless Functional Components


040 The Stateless Functional Component

example 1

1
2
3
4
5
6
7
8
9
10
const User = (props) => {
    return (
        <div>
            <p>Name: {props.name}</p>
            <p>Age: {props.age}</p>
        </div>
    )
}

ReactDOM.render(<User name="Andrew" age={26}, document.getElementById('app'));



example 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class IndecisionApp extends React.Component {

    constructor(props){
        super(props);
        this.handleDeleteOptions = this.handleDeleteOptions.bind(this);
        this.handlePick = this.handlePick.bind(this);
        this.handleAddOption = this.handleAddOption.bind(this);
        


        this.state = {
            options: ['Thing one', 'Thing two', 'Thing three']
        }
    }

    handleDeleteOptions(){
        this.setState(() => {
            return {
                options: []
            };
        });
    }

    handlePick(){
       const randomNum = Math.floor(Math.random() * this.state.options.length);
       const option = this.state.options[randomNum];
       alert(option);
    }

    handleAddOption(option){

        if (!option) {
            return 'Enter valid value to add item';
        } else if (this.state.options.indexOf(option) > -1) {
            return 'This option already exists';
        }

        this.setState((prevState) => {
            return {
                options: prevState.options.concat(option)
            }
        });
    }


    render() {

        const title = 'Indecision';
        const subtitle = 'Put your life in the hand of a computer';

        return  (
            <div>
              <Header title={title} subtitle={subtitle} />
                <Action 
                    hasOptions={this.state.options.length > 0}
                    handlePick={this.handlePick}
                />
                <Options 
                    options={this.state.options} 
                    handleDeleteOptions={this.handleDeleteOptions}
                />
              <AddOption 
                handleAddOption={this.handleAddOption}
              />
            </div>  
          );
    }
}


const Header = (props) => {
    return  (
        <div>
          <h1>{props.title}</h1>
          <h2>{props.subtitle}</h2>
        </div>  
      );
};


const Action = (props) => {
    return (
        <div>
          <button 
              onClick={props.handlePick} 
              disabled={!props.hasOptions}
           >
              What should i do?
          </button>
        </div>  
      );
};


const Options = (props) => {
    return (
        <div>
          <button onClick={props.handleDeleteOptions} >Remove All</button>
        {
          props.options.map((option) => <Option key={option} optionText={option} />)
        }
        </div>  
    );
};


const Option = (props) => {
    return (
        <div>
          Option: {props.optionText}
        </div>  
      );
};


class AddOption extends React.Component {

    constructor(props) {
        super(props);
        this.handleAddOption = this.handleAddOption.bind(this);
        this.state = {
            error: undefined
        };
    }

    handleAddOption(e) {
       e.preventDefault();
       
       const option = e.target.elements.option.value.trim();
       const error = this.props.handleAddOption(option);

       this.setState(() => {
           return { error };
       });

    }

    render() {
        return (
          <div>
          {this.state.error && <p>{this.state.error}</p>}
            <form onSubmit={this.handleAddOption} >
                <input type="text" name="option" />
                <button>Add Option</button>
            </form>
          </div>  
        );
    }
}

const jsx = (
    <div>
        <IndecisionApp />
    </div>
);


ReactDOM.render(jsx, document.getElementById('app'));


041 Default Prop Values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
class IndecisionApp extends React.Component {

    constructor(props){
        super(props);
        this.handleDeleteOptions = this.handleDeleteOptions.bind(this);
        this.handlePick = this.handlePick.bind(this);
        this.handleAddOption = this.handleAddOption.bind(this);
        
        this.state = {
            options: props.options
        }
    }

    handleDeleteOptions(){
        this.setState(() => {
            return {
                options: []
            };
        });
    }

    handlePick(){
       const randomNum = Math.floor(Math.random() * this.state.options.length);
       const option = this.state.options[randomNum];
       alert(option);
    }

    handleAddOption(option){

        if (!option) {
            return 'Enter valid value to add item';
        } else if (this.state.options.indexOf(option) > -1) {
            return 'This option already exists';
        }

        this.setState((prevState) => {
            return {
                options: prevState.options.concat(option)
            }
        });
    }


    render() {

        const subtitle = 'Put your life in the hand of a computer';

        return  (
            <div>
              <Header />
                <Action 
                    hasOptions={this.state.options.length > 0}
                    handlePick={this.handlePick}
                />
                <Options 
                    options={this.state.options} 
                    handleDeleteOptions={this.handleDeleteOptions}
                />
              <AddOption 
                handleAddOption={this.handleAddOption}
              />
            </div>  
          );
    }
}

IndecisionApp.defaultProps = {
    options: []
};

const Header = (props) => {
    return  (
        <div>
          <h1>{props.title}</h1>
          {props.subtitle && <h2>{props.subtitle}</h2>}
        </div>  
      );
};


Header.defaultProps = {
    title: 'Indecision'
};

const Action = (props) => {
    return (
        <div>
          <button 
              onClick={props.handlePick} 
              disabled={!props.hasOptions}
           >
              What should i do?
          </button>
        </div>  
      );
};


const Options = (props) => {
    return (
        <div>
          <button onClick={props.handleDeleteOptions} >Remove All</button>
        {
          props.options.map((option) => <Option key={option} optionText={option} />)
        }
        </div>  
    );
};


const Option = (props) => {
    return (
        <div>
          Option: {props.optionText}
        </div>  
      );
};


class AddOption extends React.Component {

    constructor(props) {
        super(props);
        this.handleAddOption = this.handleAddOption.bind(this);
        this.state = {
            error: undefined
        };
    }

    handleAddOption(e) {
       e.preventDefault();
       
       const option = e.target.elements.option.value.trim();
       const error = this.props.handleAddOption(option);

       this.setState(() => {
           return { error };
       });

    }

    render() {
        return (
          <div>
          {this.state.error && <p>{this.state.error}</p>}
            <form onSubmit={this.handleAddOption} >
                <input type="text" name="option" />
                <button>Add Option</button>
            </form>
          </div>  
        );
    }
}


const jsx = (
    <div>
        <IndecisionApp options={['One', 'Two']}/>
    </div>
);

ReactDOM.render(jsx, document.getElementById('app'));


043 Removing Individual Options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class IndecisionApp extends React.Component {

    constructor(props){
        super(props);
        this.handleDeleteOptions = this.handleDeleteOptions.bind(this);
        this.handlePick = this.handlePick.bind(this);
        this.handleAddOption = this.handleAddOption.bind(this);
        this.handleDeleteOption = this.handleDeleteOption.bind(this);
        
        
        this.state = {
            options: props.options
        }
    }

    handleDeleteOptions(){
        this.setState(() => ({ options: []}));
    }


    handleDeleteOption(optionToRemove){
        this.setState((prevState) => ({
            options: prevState.options.filter((option) => optionToRemove !== option)
        }));
    }

    handlePick(){
       const randomNum = Math.floor(Math.random() * this.state.options.length);
       const option = this.state.options[randomNum];
       alert(option);
    }

    handleAddOption(option){

        if (!option) {
            return 'Enter valid value to add item';
        } else if (this.state.options.indexOf(option) > -1) {
            return 'This option already exists';
        }

        this.setState ((prevState) => ({ options: prevState.options.concat(option) }));
    }


    render() {

        const subtitle = 'Put your life in the hand of a computer';

        return  (
            <div>
              <Header />
                <Action 
                    hasOptions={this.state.options.length > 0}
                    handlePick={this.handlePick}
                />
                <Options 
                    options={this.state.options} 
                    handleDeleteOptions={this.handleDeleteOptions}
                    handleDeleteOption={this.handleDeleteOption}
                />
                <AddOption 
                    handleAddOption={this.handleAddOption}
                />
            </div>  
          );
    }
}

IndecisionApp.defaultProps = {
    options: []
};

const Header = (props) => {
    return  (
        <div>
          <h1>{props.title}</h1>
          {props.subtitle && <h2>{props.subtitle}</h2>}
        </div>  
      );
};


Header.defaultProps = {
    title: 'Indecision'
};

const Action = (props) => {
    return (
        <div>
          <button 
              onClick={props.handlePick} 
              disabled={!props.hasOptions}
           >
              What should i do?
          </button>
        </div>  
      );
};


const Options = (props) => {
    return (
        <div>
          <button onClick={props.handleDeleteOptions} >Remove All</button>
        {
          props.options.map((option) => (

              <Option 
                key={option} 
                optionText={option} 
                handleDeleteOption={props.handleDeleteOption}
              />
            ))
        }
        </div>  
    );
};


const Option = (props) => {
    return (
        <div>
          Option: {props.optionText}
          <button 
                onClick={(e) => {
                    props.handleDeleteOption(props.optionText);
                }}
                
                >
                remove
           </button>
        </div>  
      );
};


class AddOption extends React.Component {

    constructor(props) {
        super(props);
        this.handleAddOption = this.handleAddOption.bind(this);
        this.state = {
            error: undefined
        };
    }

    handleAddOption(e) {
       e.preventDefault();
       
       const option = e.target.elements.option.value.trim();
       const error = this.props.handleAddOption(option);

       this.setState(() => ({ error }));

    }

    render() {
        return (
          <div>
          {this.state.error && <p>{this.state.error}</p>}
            <form onSubmit={this.handleAddOption} >
                <input type="text" name="option" />
                <button>Add Option</button>
            </form>
          </div>  
        );
    }
}


const jsx = (
    <div>
        <IndecisionApp options={['One', 'Two']}/>
    </div>
);



ReactDOM.render(jsx, document.getElementById('app'));


045 Saving and Loading Options Data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class IndecisionApp extends React.Component {

    constructor(props){
        super(props);
        this.handleDeleteOptions = this.handleDeleteOptions.bind(this);
        this.handlePick = this.handlePick.bind(this);
        this.handleAddOption = this.handleAddOption.bind(this);
        this.handleDeleteOption = this.handleDeleteOption.bind(this);
        
        
        this.state = {
            options: props.options
        }
    }

    componentDidMount() {

        try {

            const json = localStorage.getItem('options');
            const options = JSON.parse(json);

            if (options) {
                this.setState(() => ({ options }));            
            }

        } catch (e) {
            // Do nothing at all
        }

    }

    componentDidUpdate(prevProps, prevState) {
        if (prevState.options.length !== this.state.options.length) {
            const json = JSON.stringify(this.state.options);
            localStorage.setItem('options', json);
        }
    }

    componentWillUnmount() {
        console.log('componentWillUnmount');
    }


    handleDeleteOptions(){
        this.setState(() => ({ options: []}));
    }


    handleDeleteOption(optionToRemove){
        this.setState((prevState) => ({
            options: prevState.options.filter((option) => optionToRemove !== option)
        }));
    }

    handlePick(){
       const randomNum = Math.floor(Math.random() * this.state.options.length);
       const option = this.state.options[randomNum];
       alert(option);
    }

    handleAddOption(option){

        if (!option) {
            return 'Enter valid value to add item';
        } else if (this.state.options.indexOf(option) > -1) {
            return 'This option already exists';
        }

        this.setState ((prevState) => ({ options: prevState.options.concat(option) }));
    }


    render() {

        const subtitle = 'Put your life in the hand of a computer';

        return  (
            <div>
              <Header />
                <Action 
                    hasOptions={this.state.options.length > 0}
                    handlePick={this.handlePick}
                />
                <Options 
                    options={this.state.options} 
                    handleDeleteOptions={this.handleDeleteOptions}
                    handleDeleteOption={this.handleDeleteOption}
                />
                <AddOption 
                    handleAddOption={this.handleAddOption}
                />
            </div>  
          );
    }
}

IndecisionApp.defaultProps = {
    options: []
};

const Header = (props) => {
    return  (
        <div>
          <h1>{props.title}</h1>
          {props.subtitle && <h2>{props.subtitle}</h2>}
        </div>  
      );
};


Header.defaultProps = {
    title: 'Indecision'
};

const Action = (props) => {
    return (
        <div>
          <button 
              onClick={props.handlePick} 
              disabled={!props.hasOptions}
           >
              What should i do?
          </button>
        </div>  
      );
};


const Options = (props) => {
    return (
        <div>
          <button onClick={props.handleDeleteOptions} >Remove All</button>
          {props.options.length === 0 && <p>Please add an option to get started!</p>}
        {
          props.options.map((option) => (

              <Option 
                key={option} 
                optionText={option} 
                handleDeleteOption={props.handleDeleteOption}
              />
            ))
        }
        </div>  
    );
};


const Option = (props) => {
    return (
        <div>
          Option: {props.optionText}
          <button 
                onClick={(e) => {
                    props.handleDeleteOption(props.optionText);
                }}
                
                >
                remove
           </button>
        </div>  
      );
};


class AddOption extends React.Component {

    constructor(props) {
        super(props);
        this.handleAddOption = this.handleAddOption.bind(this);
        this.state = {
            error: undefined
        };
    }

    handleAddOption(e) {
       e.preventDefault();
       
       const option = e.target.elements.option.value.trim();
       const error = this.props.handleAddOption(option);

       this.setState(() => ({ error }));

       if (!error) {
            e.target.elements.option.value = '';
       }

    }

    render() {
        return (
          <div>
          {this.state.error && <p>{this.state.error}</p>}
            <form onSubmit={this.handleAddOption} >
                <input type="text" name="option" />
                <button>Add Option</button>
            </form>
          </div>  
        );
    }
}

const jsx = (
    <div>
        <IndecisionApp options={[]}/>
    </div>
);

ReactDOM.render(jsx, document.getElementById('app'));