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

Add onExecute callback #22

Merged
merged 3 commits into from
Oct 1, 2023
Merged

Conversation

nicotsx
Copy link
Contributor

@nicotsx nicotsx commented Sep 29, 2023

Purpose

Inspired by react-query's onMutate I added the callback onExecute to both hooks useAction and useOptimisticAction. This callback is useful to run code before any action and avoid creating an extra helper function. The input can be accessed aswell in order to perform different actions based on it

Before

const action = useAction(myAction)

const mutate = () => {
    console.log("I really want to print this on action execution");
    action.execute();
}

return <button onClick={mutate}>hello</button>

After

const action = useAction(myAction, {
    onExecute(input) {
        console.log("I really want to print this on action execution", input);
    }
})

return <button onClick={action.execute}>hello</button>

@vercel
Copy link

vercel bot commented Sep 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-safe-action-example-app ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 1, 2023 3:24pm

Add a new callback for the useAction hook onExecute
Add a new callback for the useOptimisticAction hook onExecute
@TheEdoRan
Copy link
Owner

Awesome work, this will be a nice addition to the lib! Just one thing though, can you please restore the original README_v2.md file and update README.md instead? v2 is for the old 2.x.x version of next-safe-action, README.md is for version 3 (the current one). Thank you very much!

@nicotsx
Copy link
Contributor Author

nicotsx commented Oct 1, 2023

Sure thing @TheEdoRan, I've updated the correct readme!

@TheEdoRan TheEdoRan merged commit 79fad63 into TheEdoRan:main Oct 1, 2023
3 checks passed
@github-actions
Copy link

github-actions bot commented Oct 1, 2023

🎉 This PR is included in version 3.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@nicotsx nicotsx deleted the feat/add-on-execute branch October 1, 2023 17:03
@github-actions
Copy link

github-actions bot commented Oct 1, 2023

🎉 This PR is included in version 4.0.0-experimental.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants