GithubHelp home page GithubHelp logo

helendrug / power_of_react_hooks Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 111 KB

This repo is for coding session of the talk "The Power of React Hooks" for NovaSummit 2022

HTML 3.41% CSS 4.64% TypeScript 91.94%

power_of_react_hooks's Introduction

power_of_react_hooks

This repo is for coding session of the talk "The Power of React Hooks" for NovaSummit 2022

Hello everyone! In this small coding session im going to show you how to use the most commonly used React hooks. I created a simple ToDo app to demonstrate the usage of the hooks. What I have in the app is just a list of tasks and a form to add new tasks. The app is not connected to any backend, so the tasks are not persisted. The app is styled with Mantine UI library. //const todos: Todo[] = [{text: "Buy groceries", complete: false}, {text: "Go to the gym", complete: true}]; Since im using here only functional components, I will use React hooks to manage the state of the app. Previously when we wanted to manage the state of our app we had to use class components. Now I don't use any hooks so adding new task doesn't do anything. Let's start with the useState hook.

#1 show how useState() hook works. useState() lets you add React state to function components. To do this, we need first to import the hook from React and then declare a variable that will hold the state and the method that will update the state. Now we need to initialize the state with the initial value. By clicking on the button we still don't see any data being updated, that's why we need to call setTodos() method to update the state. #2 show how to add useEffect() - for example: The Effect Hook lets you perform side effects in function components (fetch data, re-render component, etc.). Similar to componentDidMount and componentDidUpdate.

  • if dependency array is empty (useEffect(() => { setTodos([]); },))
  • if we add something to the dependency array, what happens useEffect(() => { setTodos([]); }, [])
  • what happens if we add state as dependency in the useEffect: useEffect(() => { setTodos([]); }, [todos])
  • axios.fetch("http://localhost:8080/todos").then((response) => {response.json().then((data) => {setTodos(data);});});

#3 show how to use useCallback() useCallback will return a memoized version of the callback that only changes if one of the dependencies has changed. This is useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders (e.g. shouldComponentUpdate). useCallback(fn, deps) is equivalent to useMemo(() => fn, deps). const memoizedCallback = useCallback( () => { doSomething(a, b); }, [a, b], ); const [count, setCount] = useState(0); const addTodo: AddTodo = useCallback((text: string) => { const newTodo = {text, complete: false}; //setTodos([...todos, newTodo]); setCount(count + 1); }, [todos]) //estimation button

#4 show how to use useMemo() useMemo will only recompute the memoized value when one of the dependencies has changed. This optimization helps to avoid expensive calculations on every render.

#5 show how to use useRef()

#6 useContext() is JavaScript function, which lets you read the context and subscribe to its changes in functional component. Context is primarily used when some data needs to be accessible by many components at different nesting levels. To use this hook, you need to import it first from React library like this: import {useContext} from 'react'; Than we call useContext() function, which accepts context object as argument and returns current context value: const contextValue = useContext(MyContext);

#7 useReducer() is a hook that is used for state management. It is an alternative to useState(). Accepts a reducer of type (state, action) => newState, and returns the current state paired with a dispatch method. It is preferable to use when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one. (If you’re familiar with Redux, you already know how this works.)

power_of_react_hooks's People

Contributors

helendrug avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.