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

useAsyncEffect hook #35

Merged
merged 8 commits into from
Sep 3, 2020

Conversation

myty
Copy link
Contributor

@myty myty commented Sep 3, 2020

  • N/A Related GitHub issue(s) linked in PR description
  • Destination branch merged, built and tested with your changes
  • Code formatted and follows best practices and patterns
  • Code builds cleanly (no additional warnings or errors)
  • Manually tested
  • Automated tests are passing
  • Increased No decreases in automated test coverage
  • Documentation updated (readme, docs, comments, etc.)
  • N/A Localization: No hard-coded error messages in code files (minimally in string constants)

This hook takes the common pattern of executing an async function in a useEffect hook and cleans it up a little bit.

So something like this:

const Component = ({ id }) => {
    const [data, setData] = useState();

    useEffect(() => {
        const getData = async (id: string) => {
            const response = await Service.get({ id });
            setData(response.data);
        };
        getData(id);
    }, [id, setData]);
};

becomes this:

const Component = ({ id }) => {
    const [data, setData] = useState();

    useAsyncEffect(async (id: string) => {
        const response = await Service.get({ id });
        setData(response.data);
    }, [id, setData]);
};

@codecov
Copy link

codecov bot commented Sep 3, 2020

Codecov Report

Merging #35 into master will increase coverage by 0.40%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #35      +/-   ##
==========================================
+ Coverage   93.65%   94.06%   +0.40%     
==========================================
  Files          12       13       +1     
  Lines         205      219      +14     
  Branches       23       25       +2     
==========================================
+ Hits          192      206      +14     
  Misses         12       12              
  Partials        1        1              
Impacted Files Coverage Δ
src/hooks/use-async-effect.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a62463a...2c14214. Read the comment docs.

@myty myty marked this pull request as ready for review September 3, 2020 12:00
@myty
Copy link
Contributor Author

myty commented Sep 3, 2020

I think there could possibly be better testing around dependency changes, but it's a good start.

@brandongregoryscott brandongregoryscott requested a review from a team September 3, 2020 12:14
@brandongregoryscott brandongregoryscott added the enhancement New feature or request label Sep 3, 2020
Copy link
Contributor

@brandongregoryscott brandongregoryscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Just a few minor comments to cleanup

src/hooks/use-async-effect.test.tsx Outdated Show resolved Hide resolved
src/hooks/use-async-effect.test.tsx Outdated Show resolved Hide resolved
@myty myty requested a review from a team September 3, 2020 12:38
Copy link

@dylanjustice dylanjustice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea!

@brandongregoryscott
Copy link
Contributor

Awesome idea for a hook, thanks @myty!

@brandongregoryscott brandongregoryscott merged commit 93d00e6 into rsm-hcd:master Sep 3, 2020
@brandongregoryscott
Copy link
Contributor

Published with v0.1.4

@brandongregoryscott
Copy link
Contributor

@all-contributors add @myty for code, test

@allcontributors
Copy link
Contributor

@brandongregoryscott

I've put up a pull request to add @myty! 🎉

@brandongregoryscott
Copy link
Contributor

@all-contributors add @dylanjustice for review

@allcontributors
Copy link
Contributor

@brandongregoryscott

I've put up a pull request to add @dylanjustice! 🎉

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

Successfully merging this pull request may close these issues.

3 participants