An idea for hooks without nesting...
See original GitHub issueWith new hooks support, accessing formState and formApi still requires you nest a custom component within your form. That’s really just reimplementing HOC behavior instead of helping to flatten react’s logic. Informed is so great because it’s so flat. But wouldn’t it be great if you could do something like this?
function CustomForm() {
const formApi = useFormApi();
return (
<Form api={formApi}>
<Select onBlur={() => formApi.submitForm()}/>
</Form>
);
}
At present, the closest you can do is the following, but it feels like this behavior should really be part of Informed
, not a custom ref that isn’t even guaranteed to be there on first load.
function CustomForm() {
const formApi = useRef();
return (
<Form getApi={(api) => {
formApi.current = api;
}}>
<Select onBlur={() => formApi.current.submitForm()}/>
</Form>
);
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Entry Makeover with Removable Wallpaper and No Closet
Entry Ideas with No Closet | Nesting With Grace | Struggling with ... Get my top storage tips using baskets and hooks and...
Read more >15 Things to Do With Command Hooks in Your Home
Command hooks are becoming increasingly popular because you can stick them anywhere and easily remove them without damaging the wall or ...
Read more >10 Mind Blowing Command Hook Hacks - A Cultivated Nest
Remove without peeling paint. Use a hairdryer and floss to help remove the command adhesive without peeling paint. 1. Command Hook Necklace Organizing....
Read more >50 of the Best Command Hook Hacks - Edit + Nest
This is the complete guide to Command Hook hacks. Learn how to organize and decorate your entire home with the ideas and tips...
Read more >Hooks FAQ - React
But in most cases, Hooks will be sufficient and can help reduce nesting in your tree.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is done and in latest release!!! Need to add docs but there is now a
useForm
hook and also aFormProvider
component!!Hey @mielecmichal,
setState
immediately triggers a re-render. In the case of formState, that’ll work most of the time. Unless you do something like—useEffect(() => changeInput(), [formState])
—to trigger infinite renders. But in the case ofuseState()
forformApi
, I ran into a similar loop. It’s possible I could have avoided it withuseCallback()
with<Form getApi={callback}/>
, but I can’t remember what I tested when I chose to go withuseRef()
. But if what you’re doing works, that’s all that matters!