Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. import React, { useState } from 'react';
  2. import Person from './Person/Person';
  3. import './App.css';
  4.  
  5. //class App extends Component {
  6. const app = props => {
  7. const [personsState,setPersonState] = useState({
  8. persons: [
  9. {name:'mani',age:45},
  10. {name:'papi',age:34}
  11. ]
  12. });
  13.  
  14.  
  15. const [otherState,setOtherState] = useState({otherState:' other state'})
  16.  
  17. console.log(personsState,otherState);
  18.  
  19. const switchNameHandler = () => {
  20. console.log('switch clicked');
  21.  
  22. setPersonState({
  23. persons: [
  24. { name: 'manivel', age: 45 },
  25. { name: 't bag', age: 34 }
  26. ]
  27. });
  28.  
  29. // React hooks, manipulate the state in functions.
  30. };
  31.  
  32. //render() {
  33. return (
  34. <div className="App">
  35. <h1>Hi, I am a React App</h1>
  36. <Person name={personsState.persons[0].name} age={personsState.persons[0].age}/>
  37. <Person name={personsState.persons[1].name} age={personsState.persons[1].age}> and my hobbier are:studying </Person>
  38. <button onClick={switchNameHandler}> Click me</button>
  39. </div>
  40. );
  41.  
  42. //return React.createElement('div',{className:'App'}, React.createElement('h1',null,'Hi, I am mannie'));
  43. //}
  44. };
  45.  
  46. export default app;
  47.  
  48. // Functional based component: ( After react 16.8) - React hooks
  49. // React hook defination: Collection of function exposed by react to used to mange the state.
  50. // It will not have render
  51. // import {useState} - Manage the state in function component
  52. // Use state uses the state and returs the exactly two elements.
  53. // First element is current state, second state is always update state to re-render the element
  54. // set person allows to set the state.
  55. // Notice that functional component allows function with in a function.
  56. // call the setPerson to set the state.
  57. // extract the state, updating the state and rerending the state.
  58. // use usestate to merge the old state Object/value automatically.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement