Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useThis hook #9

Open
3 tasks
Ruben-Arushanyan opened this issue Oct 14, 2022 · 1 comment
Open
3 tasks

useThis hook #9

Ruben-Arushanyan opened this issue Oct 14, 2022 · 1 comment
Assignees
Labels

Comments

@Ruben-Arushanyan
Copy link
Owner

Ruben-Arushanyan commented Oct 14, 2022

const _this = useThis()

_this.state
_this.setState()
_this.forceUpdate()
  • code
  • docs
  • manual test using docs
@Ruben-Arushanyan
Copy link
Owner Author

Old version:

import {useRef, useEffect} from 'react';
import _isObject from 'lodash/isObject';
import {useConstructor} from 'Hooks';

const useThis = (initialThis) => {

    if (!_isObject(initialThis)) {
        initialThis = {};
    }

    const renderCount = useRef(0);

    const {current} = useRef(initialThis);

    useConstructor(() => {
        current._isFirstRender = true;
    });

    useEffect(() => {
        renderCount.current++;
        current._isFirstRender = renderCount.current < 2;
    });

    return current;
};

export {useThis};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant