GithubHelp home page GithubHelp logo

ebazhanov / i-love-userreducer Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 364 KB

Several examples of how to `useReducer`

Home Page: https://reactjs.org/docs/hooks-reference.html#usereducer

HTML 17.31% CSS 9.36% JavaScript 73.33%
react usereducer-hooks examples

i-love-userreducer's Introduction

Find out how to useReducer in different cases:

useReducer is an alternative to useState. useReducer is usually preferable to useState when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one.

We will start with useState and convert it later to userReducer

import React, {useState} from "react";

const App = () => {
    const [count, setCount] = useState(0);

    return (
        <div className="App">
            <header className="App-header">
                <div>
                    <p>{count}</p>
                    <button
                        type="button"
                        onClick={() => {
                            setCount(count + 1);
                        }}
                    >
                        useState Count
                    </button>
                </div>
            </header>
        </div>
    );
}

export default App;

So now the first example how we can convert useState to useReducer

import React, {useReducer} from "react";

function reducer(count) {
    return count + 1;
}

const App = () => {
    const [reducerCount, dispatch] = useReducer(reducer, 0);

    return (
        <div className="App">
            <header className="App-header">
                <div>
                    <p>{reducerCount}</p>
                    <button
                        type="button"
                        onClick={() => {
                            dispatch();
                        }}
                    >
                        REDUCER COUNT
                    </button>
                </div>
            </header>
        </div>
    );
}

export default App;

Third example with more complex example:

import React, {useReducer} from "react";

const initialState = {
    count: 0
}

function reducer(state, action) {
    if (action === "add")
        return {
            count: state.count + 1
        };
    return {
        count: state.count - 1
    }
}

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

    return (
        <div className="App">
            <header className="App-header">
                <div>
                    <p>{state.count}</p>
                    <button
                        type="button"
                        onClick={() => {
                            dispatch("add");
                        }}
                    >
                        ADD
                    </button>
                </div>
                <div>
                    <button
                        type="button"
                        onClick={() => {
                            dispatch();
                        }}
                    >
                        SUBTRACT
                    </button>
                </div>
            </header>
        </div>
    );
}

export default App;

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.