The Complete React Web Developer Course (2nd Edition)


04 React Components


026 Creating a React Component


$ vi src/app.js


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
class Header extends React.Component {
    render() {
        return  (
            <div>
              <h1>Indecision</h1>
              <h2>Put your life in the hand of a computer</h2>
            </div>  
          );
    }
}

class Action extends React.Component {
    render() {
        return (
          <div>
            <button>What should i do?</button>
          </div>  
        );
    }
}

class Options extends React.Component {
    render() {
        return (
          <div>
            Options component here
          </div>  
        );
    }
}

class AddOption extends React.Component {
    render() {
        return (
          <div>
            AddOption component here
          </div>  
        );
    }
}


const jsx = (
    <div>
        <Header />
        <Action />
        <Options />
        <AddOption />
    </div>
);

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


027 Nesting Components

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
class IndecisionApp extends React.Component {
    render() {
        return  (
            <div>
              <Header />
              <Action />
              <Options />
              <AddOption />
            </div>  
          );
    }
}


class Header extends React.Component {
    render() {
        return  (
            <div>
              <h1>Indecision</h1>
              <h2>Put your life in the hand of a computer</h2>
            </div>  
          );
    }
}

class Action extends React.Component {
    render() {
        return (
          <div>
            <button>What should i do?</button>
          </div>  
        );
    }
}

class Options extends React.Component {
    render() {
        return (
          <div>
            Options component here
            <Option />
          </div>  
        );
    }
}

class Option extends React.Component {
    render() {
        return (
          <div>
            Option component here
          </div>  
        );
    }
}

class AddOption extends React.Component {
    render() {
        return (
          <div>
            AddOption component here
          </div>  
        );
    }
}


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

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


028 Component Props

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
class IndecisionApp extends React.Component {
    render() {

        const title = 'Indecision';
        const subtitle = 'Put your life in the hand of a computer';
        const options = ['Thing one', 'Thing two', 'Thing four'];

        return  (
            <div>
              <Header title={title} subtitle={subtitle} />
              <Action />
              <Options options={options} />
              <AddOption />
            </div>  
          );
    }
}


class Header extends React.Component {
    render() {
        return  (
            <div>
              <h1>{this.props.title}</h1>
              <h2>{this.props.subtitle}</h2>
            </div>  
          );
    }
}

class Action extends React.Component {
    render() {
        return (
          <div>
            <button>What should i do?</button>
          </div>  
        );
    }
}

class Options extends React.Component {
    render() {
        return (
          <div>
          {
            this.props.options.map((option) => <Option key={option} optionText={option} />)
          }
          </div>  
        );
    }
}

class Option extends React.Component {
    render() {
        return (
          <div>
            Option: {this.props.optionText}
          </div>  
        );
    }
}

class AddOption extends React.Component {
    render() {
        return (
          <div>
            AddOption component here
          </div>  
        );
    }
}


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

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


029 Events Methods, 030 Method Binding, 031 What Is Component State, 032 Adding State to Counter App Part I, 033 Adding State to Counter App Part II, 035 Build It Adding State to VisibilityToggle, 036 Indecision State Part I, 037 Indecision State Part II

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
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>  
          );
    }
}


class Header extends React.Component {
    render() {
        return  (
            <div>
              <h1>{this.props.title}</h1>
              <h2>{this.props.subtitle}</h2>
            </div>  
          );
    }
}

class Action extends React.Component {
    render() {
        return (
          <div>
            <button 
                onClick={this.props.handlePick} 
                disabled={!this.props.hasOptions}
             >
                What should i do?
            </button>
          </div>  
        );
    }
}

class Options extends React.Component {

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

class Option extends React.Component {
    render() {
        return (
          <div>
            Option: {this.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'));