Giter VIP home page Giter VIP logo

usemembers's Introduction

useMembers

React.js hook for moving callbacks out of a functional component into a separate class

Before

interface IProps {
    prop1: number;
    prop2?: string;
}

export function SomeComponent(props: IProps) {
    const { prop1, prop2 } = props;
    const [value1, setValue1] = React.useState(0);
    const [value2, setValue2] = React.useState("some text");

    const onEvent1 = React.useCallback(() => {
        // ... some code using prop1 and value1 
        // setValue1(evalutedValue)
    }, [prop1, value1]);
    const onEvent2 = React.useCallback((arg: string) => {
        // ... some code using arg, prop1, prop2 and value1 
        // setValue2(evalutedValue)
    }, [prop1, prop2, value2]);

    React.useEffect(() => {
        // some code
    });

    return <AnotherComponent value1={value1} value2={value2} onEvent1={onEvent1} onEvent2={onEvent2}>
        {/* ... some children */}
    </AnotherComponent>;
}

After

interface IProps {
    prop1: number;
    prop2?: string;
}

export function SomeComponent(props: IProps) {
    const { prop1, prop2 } = props;
    const [value1, setValue1] = React.useState(0);
    const [value2, setValue2] = React.useState("some text");
    const {onEvent1, onEvent2, afterRender} = useMembers(Members, {props, value1, setValue1, value2, setValue2});
    
    React.useEffect(afterRender);

    return <AnotherComponent value1={value1} value2={value2} onEvent1={onEvent1} onEvent2={onEvent2}>
        {/* ... some children */}
    </AnotherComponent>;
}

interface IDeps {
    props: IProps;
    value1: number;
    setValue1: React.Dispatch<React.SetStateAction<number>>;
    value2: string;
    setValue2: React.Dispatch<React.SetStateAction<string>>;
}

class Members extends MembersBase<IDeps> {
    onEvent1 = () => {
        const {props: {prop1}, value1, setValue1} = this.deps;
        // ... some code using prop1 and value1 
        // setValue1(evalutedValue)
    };
    onEvent2 = (arg: string) => {
        const {props: {prop1, prop2}, value2, setValue2} = this.deps;
        // ... some code using arg, prop1, prop2 and value1 
        // setValue2(evalutedValue)
    };
    afterRender = () => {
        // some code
    };
}

usemembers's People

Contributors

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