question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

An idea for hooks without nesting...

See original GitHub issue

With 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
joepuzzocommented, May 2, 2019

This is done and in latest release!!! Need to add docs but there is now a useForm hook and also a FormProvider component!!

2reactions
mikestopcontinuescommented, Feb 13, 2019

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 of useState() for formApi, I ran into a similar loop. It’s possible I could have avoided it with useCallback() with <Form getApi={callback}/>, but I can’t remember what I tested when I chose to go with useRef(). But if what you’re doing works, that’s all that matters!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found