|
| 1 | +--- |
| 2 | +id: state-hoisting |
| 3 | +sidebar_label: State hoisting |
| 4 | +title: State Hoisting |
| 5 | +description: State hoisting | React Patterns, techniques, tips and tricks in development for Ract developer. |
| 6 | +keywords: ['state hoisting', 'reactpatterns', 'react patterns', 'reactjspatterns', 'reactjs patterns', 'react', 'reactjs', 'react techniques', 'react tips and tricks'] |
| 7 | +version: State hoisting |
| 8 | +image: /img/reactpatterns-cover.png |
| 9 | +--- |
| 10 | + |
| 11 | +As the application grows you will realise that some components will need common data or actions in one component may need to cause another component to re-render as well. |
| 12 | + |
| 13 | +Let take a look the example below. |
| 14 | + |
| 15 | +```jsx |
| 16 | +// App.js |
| 17 | + |
| 18 | +import React from 'react' |
| 19 | +import Header from 'Header' |
| 20 | +import List from 'List' |
| 21 | + |
| 22 | +export class App extends React.Component { |
| 23 | + render(){ |
| 24 | + return( |
| 25 | + <div> |
| 26 | + <Header/> |
| 27 | + <List/> |
| 28 | + </div> |
| 29 | + ) |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +```jsx |
| 35 | +// Header.js |
| 36 | + |
| 37 | +import React from 'react' |
| 38 | + |
| 39 | +export class Header extends React.Component { |
| 40 | + constructor(props) { |
| 41 | + super(props); |
| 42 | + this.state = {customers: []} |
| 43 | + } |
| 44 | + |
| 45 | + componentDidMount() { |
| 46 | + const customers = getData() // this fictional function calls an API |
| 47 | + this.setState({customers}) |
| 48 | + } |
| 49 | + |
| 50 | + render() { |
| 51 | + return(<div>No of Customer: {this.state.customers.length}</div>) |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +```jsx |
| 57 | +// List.js |
| 58 | + |
| 59 | +import React from 'react' |
| 60 | + |
| 61 | +export class List extends React.Component { |
| 62 | + constructor(props) { |
| 63 | + super(props) |
| 64 | + this.state = {customers: []} |
| 65 | + } |
| 66 | + |
| 67 | + componentDidMount() { |
| 68 | + const cars = getData() // this fictional function calls an API |
| 69 | + this.setState({customers}) |
| 70 | + } |
| 71 | + |
| 72 | + render() { |
| 73 | + return this.state.customers.map((car) => { |
| 74 | + return (<div>Customer: {customers.name}</div>) |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +As the example above you are encouraged to lift state up, if two components need to act on the same data or need to use the same callback. |
| 81 | + |
| 82 | +So you should create a common ancestor in this common ancestor and then use the state to manage all the data and callbacks that children will use in rendering as following. |
| 83 | + |
| 84 | +```jsx |
| 85 | +// App.js |
| 86 | + |
| 87 | +import React from 'react' |
| 88 | +import Header from 'Header' |
| 89 | +import List from 'List' |
| 90 | + |
| 91 | +export class App extends React.Component { |
| 92 | + constructor(props){ |
| 93 | + super(props) |
| 94 | + this.state = {customers: []} |
| 95 | + } |
| 96 | + |
| 97 | + componentDidMount() { |
| 98 | + const customers = getData() // this fictional function calls an API |
| 99 | + this.setState({customers}) |
| 100 | + } |
| 101 | + |
| 102 | + render(){ |
| 103 | + const {customers} = this.state |
| 104 | + |
| 105 | + return( |
| 106 | + <div> |
| 107 | + <Header customers/> |
| 108 | + <List customers/> |
| 109 | + </div> |
| 110 | + ) |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +```jsx |
| 116 | +// Header.js |
| 117 | + |
| 118 | +const Header = ({customers}) => (<div> No of Customer: {customers.length}</div>) |
| 119 | + |
| 120 | +export Header |
| 121 | +``` |
| 122 | + |
| 123 | +```jsx |
| 124 | +// List.js |
| 125 | + |
| 126 | +const List = ({customers}) => ( |
| 127 | + customers.map((customer) => { |
| 128 | + return (<div>Customer: {customer.name}</div>) |
| 129 | + }) |
| 130 | +) |
| 131 | + |
| 132 | +export List |
| 133 | +``` |
0 commit comments