1 of 10

2 of 10

React Hooks Part 1

Viktor Nawrath

1

Oct/2022

3 of 10

React Component

  • main building block in React�
  • define how UI will look like (appearance) and behave (logic)

  • receives props (external, static) and can have some state (internal, dynamic)

  • can be composed of other components

2

Oct/2022

import { CounterDisplay } from "./components/CounterDisplay";

export default function App(props) {

const [counter, setCounter] = useState(props.initialCount);

return (

<div>

<CounterDisplay count={counter} />

<button onClick={() => setCounter(counter + 1)}>Increment</button>

</div>

);

}

export function CounterDisplay(props) {

const { count } = props;

return <p>{count}</p>;

}

4 of 10

Functional component lifecycle

  • gets called on every render

    • re-rendered everytime a state changes, or parent re-renders�
    • state, effects and other logic is handled using hooks�
    • useState hook for state management�
    • useEffect hook for side-effects (APIs, timers, etc.)

3

Oct/2022

export default function App(props) {

const [counter, setCounter] = useState(0);

useEffect(() => {

const interval = setInterval(

() => setCounter((c) => c + 1),

1000

);

return () => clearInterval(interval);

}, []);

return (

<div>

<CounterDisplay count={counter} />

</div>

);

}

5 of 10

React hooks

  • adding all the functionality from classes to functional components�
  • always start with `use`�
  • many are built-in, but you can create your own�
  • need to be called unconditionally, always in the same order

4

Oct/2022

6 of 10

BETA React docs

  • still in BETA, but most important APIs are already documented

  • great resource to learn how to “think in React”

  • centered around functional components

  • explain common mistakes made when using hooks

5

Oct/2022

7 of 10

useState hook

  • let’s a component “remember” some information

6

Oct/2022

8 of 10

useState hook

7

Oct/2022

// You can set the initial value as a first argument

const [name, setName] = useState("Jane");

// You can provide the initial value as a function,

// the function will only get called on first render

const [balance, setBalance] = useState(() => expensiveFunction());

// You can then set the value using the returned setter

// Either by providing the new value

setName("Marie")

// Or using a function, that receives the previous state as argument

setBalance((previousBalance) => previousBalance + 420)

9 of 10

useReducer hook

  • useState on steroids

  • can be used to build state machines

  • builds on the “Flux” pattern (also look-up the “redux” framework)

8

Oct/2022

const initialState = {count: 0};

function reducer(state, action) {

switch (action.type) {

case 'increment':

return {count: state.count + 1};

case 'decrement':

return {count: state.count - 1};

default:

throw new Error();

}

}

function Counter() {

const [state, dispatch] = useReducer(reducer, initialState);

return (

<>

Count: {state.count}

<button onClick={() => dispatch({type: 'decrement'})}>-</button>

<button onClick={() => dispatch({type: 'increment'})}>+</button>

</>

);

}

10 of 10

text

9

Sep/2021

Thank you for your attention

Petr Večeřa

+420 775 350 603

petr.vecera@vsb.cz

www.vsb.cz