Skip to content

Commit 3d04f20

Browse files
committed
ready
1 parent d799d55 commit 3d04f20

File tree

5 files changed

+60
-26
lines changed

5 files changed

+60
-26
lines changed

firstapp/src/Components/Cockpit/Cockpit.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
22
import styles from './Cockpit.module.css';
3-
import Aux from '../../hoc/Aux';
43
const cockpit = (props) => {
54

65
const assignedClasses = [];
@@ -27,13 +26,17 @@ const cockpit = (props) => {
2726

2827
return(
2928
<>
30-
<h1>tekstä</h1>
29+
<h1>{props.appTitle }</h1>
3130
<p className={assignedClasses.join(' ')}>lisää tekstiä</p>
3231

3332
<button
3433
className={btnClass}
3534
onClick={props.clicked}> Toggle Persons </button>
35+
<button
36+
onClick={props.login }>
37+
Log In
38+
</button>
3639
</>
3740
)
3841
};
39-
export default cockpit;
42+
export default React.memo(cockpit);

firstapp/src/Components/persons/Persons.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class Persons extends PureComponent{
1515

1616
componentDidMount(){
1717
console.log('[Persons.js] Inside componentWillMount ');
18+
console.log('lastpersonref',React.createRef());
1819
this.lastPersonRef.current.focus();
20+
1921
}
2022

2123
componentWillReceiveProps(nextProps){
@@ -43,12 +45,14 @@ componentDidUpdate(){
4345
console.log('[Persons.js] inside render');
4446
return this.props.persons.map( (person, index) => {
4547
return <Person
46-
click={() => this.props.clicked( index )}
47-
nimi={ person.nimi }
48+
click = {() => this.props.clicked( index )}
49+
nimi = { person.nimi }
4850
sijainti = { index }
4951
ref = { this.lastPersonRef }
50-
key={ person.id }
51-
changed={( event ) => this.props.changed( event, person.id )}
52+
authenticated = { this.props.isAuthenticated }
53+
key = { person.id }
54+
changed = {( event ) => this.props.changed( event, person.id )}
55+
isAuthenticated = {this.props.authenticated}
5256
/>
5357

5458
} );

firstapp/src/Components/persons/person/Person.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
3-
43
import withClass from '../../../hoc/WithClass'
54
import styles from './Person.module.css';
5+
import { AuthContext } from '../../../containers/App'
66

77
class Person extends Component {
88
constructor(props){
@@ -24,19 +24,16 @@ this.inputElement = React.createRef();
2424

2525
}
2626
focus(){
27-
this.inputElement.current.focus();
27+
this.inputElement.current.focus();
2828
}
2929

3030
render(){
3131
console.log('[Person.js] inside render');
32-
// const rnd = Math.random();
33-
// console.log('rnd:', rnd);
34-
// if ( rnd > 0.7 ) {
35-
// throw new Error ( 'Something went wrong' );
36-
// }
37-
3832
return (
3933
<>
34+
<AuthContext.Consumer>
35+
{auth => auth ? <p>Authenticated!</p> : <p> :( </p> }
36+
</AuthContext.Consumer>
4037
<p onClick={this.props.click}>I am {this.props.nimi} and I am {4} years old!</p>
4138
<p>{this.props.children}</p>
4239
<input

firstapp/src/containers/App.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import Cockpit from '../Components/Cockpit/Cockpit'
88
import WithClass from '../hoc/WithClass';
99
// import ErrorBoundary from './ErrorBoundary/ErrorBoundary';
1010

11+
export const AuthContext = React.createContext(false);
12+
13+
1114
class App extends PureComponent {
1215
constructor(props){
1316
super(props);
@@ -34,8 +37,17 @@ class App extends PureComponent {
3437
// }
3538

3639
componentWillUpdate(nextProps,nextState){
37-
console.log('Inside componentWillUpdate, nextProps');
40+
console.log('Inside componentWillUpdate', nextProps, nextState);
41+
42+
}
43+
44+
static getDerivedStateFromProps(nextProps, prevState){
45+
console.log('[UPDATE App.js] Inside getDerivedStateFromProps ', nextProps, prevState);
46+
return prevState;
47+
}
3848

49+
getSnapshotBeforeUpdate(){
50+
console.log('[UPDATE App.js] Inside getSnapshotBeforeUpdate');
3951
}
4052

4153
componentDidUpdate(){
@@ -50,7 +62,8 @@ class App extends PureComponent {
5062
],
5163
otherState: 'joku muu value',
5264
showPersons: false,
53-
timesClicked: 0
65+
timesClicked: 0,
66+
authenticated: false
5467
}
5568

5669
nimiChangedHandler = (event, id) => {
@@ -72,6 +85,11 @@ class App extends PureComponent {
7285
console.log('id:',id);
7386
}
7487

88+
loginHandler = () => {
89+
this.setState({authenticated: true })
90+
91+
}
92+
7593
togglePersonsHandler = () => {
7694
let personsLength = this.state.persons.length
7795
console.log('personsLength',personsLength);
@@ -103,9 +121,11 @@ class App extends PureComponent {
103121

104122
persons = (
105123
<div>
106-
<Persons persons = {this.state.persons}
124+
<Persons
125+
persons = {this.state.persons}
107126
clicked={this.deletePersonHandler}
108127
changed={this.nimiChangedHandler}
128+
109129
/>
110130
</div>
111131
);
@@ -122,8 +142,12 @@ class App extends PureComponent {
122142
showPersons={this.state.showPersons}
123143
persons={this.state.persons}
124144
clicked={this.togglePersonsHandler}
145+
login={this.loginHandler}
125146
/>
147+
<AuthContext.Provider
148+
value={this.state.authenticated}>
126149
{persons}
150+
</AuthContext.Provider>
127151
</div>
128152
);
129153

firstapp/src/hoc/WithClass.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import React, { Component } from 'react';
22

33
const withClass = (WrappedComponent, className) => {
4-
return class extends Component {
5-
render() {
6-
return (
7-
<div className={className}>
8-
<WrappedComponent {...this.props} />
9-
</div>
10-
)
4+
const WithClass = class extends Component {
5+
render () {
6+
return (
7+
<div className={className}>
8+
<WrappedComponent ref={this.props.forwardedRef} {...this.props} />
9+
</div>
10+
)
11+
}
1112
}
12-
}
13+
14+
return React.forwardRef((props, ref) => {
15+
return <WithClass {...props} forwardedRef={ref} />
16+
});
1317
}
18+
19+
1420
export default withClass;

0 commit comments

Comments
 (0)