Giter VIP home page Giter VIP logo

react-hook-design-pattern's Introduction

useState

Step1. Firstly, let's see how to use the useState in React formlly.

import React, { useState } from 'react';
const [state, setState] = useState(initialState);

Step2. Hence we know that the useState function will return an array including two values. The first value is the target state and second one is the corresponding setState function.

let memorizedState;
function useState(initialValue) {
    // ...
    return [memorizedState, setState];
}

Step3. Furthermore, let's realize the details simply.

function render () {
    // ...
}

let memorizedState;
function useState(initialValue) {
    memorizedState = initialValue || state;
    function setState(newValue) {
        memorizedState = newValue;
        render();
    }
    return [memorizedState, setState];
}

Step4. Nevertheless, we can execute the useState several times in one application. So how to reach this? The answer is that hooks are not magic but only arrays.

function render () {
    // ...
}

let memorizedState = [];
let curIndex = 0;
function useState(initialValue) {
    memorizedState[curIndex] = initialValue || state;
    function setState(newValue) {
        memorizedState[curIndex] = newValue;
        render();
    }
    return [memorizedState[curIndex++], setState];
}

useEffect

Step1. Similarly, let's see how to use the useEffect in React formlly.

import React, { useState, useEffect } from 'react';

function Clock() {
    const [count, setCount] = useState(0);
    useEffect(()=>{
        console.log(`Show ${count} times.`);
    });
    return (
        <div> Clock Component. Totally {count} times.</div>
    );
}

Step2. As we saw above, useEffect function has a callback parameter and this callback will be executed.

function useEffect(callback) {
    callback();
}

Step3. The useEffect function can have a second parameter which should be an array standing for the dependencies. Meanwhile, we should also consider the multiple times to execute useEffect.

let effectQueue = [];
let index = 0;
function useEffect(callback, deps) {
    if (Array.isArray(deps)) {
        return 'The second parameter should be array.';
    } else if (!deps)) {
        callback();
    } else {
        const prevDeps = effectQueue[index];
        const hasChanged = deps.every((dep, index) => {
            return !(dep === prevDeps[index]);
        });
        if (hasChanged) {
            callback();
        }
        effectQueue[index] = deps;        
    }
    index++;
}

react-hook-design-pattern's People

Contributors

wannamakeudance 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.