Advertisement
Smryk

flexible changing name event

May 22nd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import './App.css';
  3. import './Person/Person.css';
  4. import Person from './Person/Person';
  5.  
  6. // We'll be creating components using this approach rarely, more often we'll be using functions that return JSX
  7. class App extends Component {
  8.  
  9.   // we can use state only in components
  10.   state = {
  11.     persons: [
  12.       { id: 'abc', name: 'Max', age: 28},
  13.       { id: 'def', name: 'Manu', age: 29},
  14.       { id: 'ghi', name: 'Stephanie', age: 26},
  15.     ],
  16.     otherState: 'some other value',
  17.     showPersons: false
  18.   }
  19.  
  20.   nameChangedHandler = (event, id) => {
  21.  
  22.       // finding if id exists (not necessary, since we pass id in arguments)
  23.  
  24.       const personIndex = this.state.persons.findIndex(p => {
  25.         return p.id === id;
  26.       });
  27.  
  28.       // copying person with found ID so we don't operate on original object
  29.  
  30.       const person = {
  31.         ...this.state.persons[personIndex]
  32.       }
  33.  
  34.       // changing name of copied person
  35.  
  36.       person.name = event.target.value;
  37.  
  38.       // copying all persons from state
  39.  
  40.       const persons = [...this.state.persons];
  41.  
  42.       // assigning person with changed name to the old one
  43.  
  44.       persons[personIndex] = person;
  45.  
  46.       // ALTERNATIVE APPROACH:
  47.       // const person = Object.assign({}, this.state.persons[personIndex])
  48.  
  49.       this.setState( { //setState() is a method from Component import
  50.         persons : persons
  51.     } )
  52.   }
  53.  
  54.   deletePersonHandler = (personIndex) => {
  55.  
  56.     // changing this persons const work, because it's an array of references
  57.     // assigning only this.state.person isn't good practice, slice() assigns copy of it
  58.     // const persons = this.state.persons.slice();
  59.  
  60.     // ES6 equivalent of slice() is spread operator(...)
  61.     const persons = [...this.state.persons];
  62.  
  63.     // deletes 1 element, starting from index = personIndex
  64.     persons.splice(personIndex, 1);
  65.     this.setState({persons: persons});
  66.   }
  67.  
  68.   togglePersonsHandler = () => {
  69.      
  70.     const doesShow = this.state.showPersons;
  71.     this.setState({
  72.       showPersons : !doesShow
  73.     })
  74.   }
  75.  
  76.   render() {
  77.  
  78.     const style = {
  79.       backgroundColor: 'white',
  80.       font: 'inherit',
  81.       border: '1px solid blue',
  82.       padding: '8px',
  83.       cursor: 'pointer'
  84.     }
  85.  
  86.     let persons = null;
  87.  
  88.     if(this.state.showPersons){
  89.       persons = (
  90.         <div>
  91.           {/* Converting JS array to JSX, index is optional, map can have one argument */}
  92.           {this.state.persons.map( (person, index) => {
  93.             return <Person
  94.               name={person.name}
  95.               age={person.age}
  96.               key={person.id}
  97.               click={() => this.deletePersonHandler(index)}
  98.               changed={(event) => this.nameChangedHandler(event, person.id)}/>
  99.  
  100.           })}
  101.         </div>
  102.       );
  103.     }
  104.  
  105.     return (
  106.       // all the code should be wrapped into one root element per component
  107.       <div className="App">
  108.         <h1>Hi, I'm a React App</h1>
  109.        <p>This is really working!</p>
  110.        {/* lowercase - native HTML elements, uppercase - our elements, e.g. <div> and <Div>*/}
  111.        {/* JS - onclick, JSX - onClick */}
  112.        {/* () => this.switchNameHandler(name) OR this.switchNameHandler.bind(this, name) */}
  113.        {/* <button onClick={() => this.switchNameHandler('test')}>Switch name</button> */}
  114.        <button
  115.          style = {style}
  116.          onClick={this.togglePersonsHandler}>Toggle persons</button>
  117.          {persons}
  118.      </div>
  119.    );
  120.    //return React.createElement('div', {class: 'App'}, React.createElement('h1', null, 'Hi, I\'m a React App!!!'));
  121.    
  122.     // both versions of code create certain elements
  123.     // in fact JSX implicitly uses React.createElement method, so we always need to remember to import React component
  124.  
  125.  
  126.   }
  127. }
  128.  
  129. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement