diff --git a/02/src/App.js b/02/src/App.js
index e32f1537..96cfda19 100644
--- a/02/src/App.js
+++ b/02/src/App.js
@@ -3,67 +3,66 @@ import './App.css';
import Person from './Person/Person';
-/* // ## Functional code generated by creat-react-app ##
-// function App() {
-// return (
-//
-//
Hi, I'm a React App, I'm cute.
-//
Created using Functional code
-//
-//
Main Character
-//
-//
-//
-// );
-// // return React.createElement('div', {className: 'App'}, React.createElement('h1', null, `Oh Hay I'm a React App!!!`));
-// }
+/*// ## Functional code generated by creat-react-app ##
+function App() {
+ return (
+
+
Hi, I'm a React App, I'm cute.
+
Created using Functional code
+
+
Main Character
+
+
+
+ );
+}
*/
-/* // ## Functional code created using ES6 (React Hooks) ##
-// const App = props => {
-// const [charState, setCharState] = React.useState({
-// character: [
-// { name: 'Yutsuo', ilvl: 320 },
-// { name: 'Schwarz', ilvl: 110 },
-// { name: 'Murom', ilvl: 78 }
-// ]
-// // somethingElse: 'something something'
-// });
-
-// const [somethingElseState, setSomethingElseState] = React.useState('yadda yadda yadda');
-
-// console.log(charState, somethingElseState);
-
-// const switchNameHandler = () => {
-// // console.log('\"CLICK\"');
-// // DON'T DO THIS: this.state.character[1].name = 'Schwarzgeist';
-// // Do not mutate state directly. Use setState() react/no-direct-mutation-state
-// setCharState({
-// character: [
-// { name: 'Yutsuo', ilvl: 320 },
-// { name: 'Schwarzgeist', ilvl: 110 },
-// { name: 'Murom', ilvl: 79 }
-// ],
-// somethingElse: charState.somethingElse
-// });
-// };
-
-// return (
-//
-//
Hi, I'm a React App, I'm cute.
-//
Created using ES6 Functional code
-//
-//
-//
-//
-//
-//
Main Character
-//
-//
-//
-// );
-// // return React.createElement('div', {className: 'App'}, React.createElement('h1', null, `Oh Hay I'm a React App!!!`));
-// }
+/*// ## Functional code created using ES6 (React Hooks) ##
+const App = props => {
+ const [charState, setCharState] = React.useState({
+ character: [
+ { name: 'Yutsuo', ilvl: 320 },
+ { name: 'Schwarz', ilvl: 110 },
+ { name: 'Murom', ilvl: 78 }
+ ]
+ // somethingElse: 'something something'
+ });
+
+ const [somethingElseState, setSomethingElseState] = React.useState('yadda yadda yadda');
+
+ console.log(charState, somethingElseState);
+
+ const switchNameHandler = () => {
+ // console.log('\"CLICK\"');
+ // DON'T DO THIS: this.state.character[1].name = 'Schwarzgeist';
+ // Do not mutate state directly. Use setState() react/no-direct-mutation-state
+ setCharState({
+ character: [
+ { name: 'Yutsuo', ilvl: 320 },
+ { name: 'Schwarzgeist', ilvl: 110 },
+ { name: 'Murom', ilvl: 79 }
+ ],
+ somethingElse: charState.somethingElse
+ });
+ };
+
+ return (
+
+
Hi, I'm a React App, I'm cute.
+
Created using ES6 Functional code
+
+
+
+
+
+
Main Character
+
+
+
+ );
+// return React.createElement('div', {className: 'App'}, React.createElement('h1', null, `Oh Hay I'm a React App!!!`));
+}
*/
/* // ## What's behind each JSX tag ##
@@ -74,39 +73,53 @@ import Person from './Person/Person';
// ## OOP code ##
class App extends React.Component {
+ // The state should be IMMUTABLE, do not directly change it
state = {
character: [
- { name: 'char1', ilvl: 0 },
- { name: 'char2', ilvl: 0 },
- { name: 'char3', ilvl: 0 }
+ { id: 1, name: 'char1', ilvl: '100' },
+ { id: 2, name: 'char2', ilvl: '110' },
+ { id: 3, name: 'char3', ilvl: '120' }
],
somethingElse: 'something something',
showOrHide: false
}
deleteCharacterHandler = (index) => {
- // const characters = this.state.character.slice();
- const characters = [...this.state.character];
- characters.splice(index, 1);
- this.setState({characters: characters});
+ const characterCopy = [...this.state.character]; //<- it's a copy so you don't change the state, but a copy of it
+ // const characters = this.state.character.slice(); //<- this works, too, but use spread [...] instead
+
+ characterCopy.splice(index, 1); // <- deletes the item from the copy of the state.character object
+ console.log('deleted character: ', this.state.character[index]);
+ this.setState({character: characterCopy}); //<- state alterations should be made by this function alone (setState())
}
- nameChangedHandler = (event) => {
- this.setState({
- character: [
- { name: 'Yutsuo', ilvl: 320 },
- { name: event.target.value, ilvl: 110 },
- { name: 'Murom', ilvl: 79 }
- ]
- })
+ nameChangedHandler = (event, id) => {
+ // find the index during input event
+ const charIndex = this.state.character.findIndex(c => {return c.id === id});
+
+ // copy the state item to manipulate it, state can't be directly changed.
+ const char = { ...this.state.character[charIndex] };
+
+ // get the input event to change name
+ char.name = event.target.value;
+
+ // make a copy of the whole characters array prepare the changes there
+ const characters = [...this.state.character];
+ characters[charIndex] = char;
+
+ // change the state
+ this.setState( {character:characters} );
}
showOrHide = () => {
const doesShow = this.state.showOrHide
+ // showOrHide was set to start false, but here when changing state it will
+ // become the opposite. So if it's true it will be false and vice-versa
this.setState({showOrHide: !doesShow})
}
render() {
+ //inline CSS directives
const style = {
backgroundColor: 'white',
font: 'inherit',
@@ -122,7 +135,14 @@ deleteCharacterHandler = (index) => {
persons = (
{this.state.character.map((char, index) => {
- return
+ return (
+
this.deleteCharacterHandler(index)}
+ name={char.name}
+ ilvl={char.ilvl}
+ key={char.id}
+ changed={(event) => this.nameChangedHandler(event, char.id)}
+ />)
})}
)
@@ -133,9 +153,9 @@ deleteCharacterHandler = (index) => {
Hi, I'm a React App, I'm cute.
Created using OOP code