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.

💡 Proposal - consistent use of React references

See original GitHub issue

Forma 36 contribution proposal

What?

👋

Forma 36 doesn’t currently handle React references in a consistent way (see #267). As we’ve talked about, v4 provides a good opportunity to take care of this. To get this consistency I suggest we always forward the reference to the root element, no matter what.

How?

For most of our components it’s pretty straightforward, we use React.forwardRef and set the ref on the outermost element in the component.

It gets a bit more complicated for some of our components. We have a lot of them wrapped in for example a <div>, and for consistency I suggest we still send the reference to that element instead of whatever component is inside it. Contrived example:

import React, { forwardRef } from 'react'

interface TextFieldProps {
  ...
}

const TextField = forwardRef<HTMLHeadingElement, TextFieldProps>(
  function TextField(props, forwardedRef) {
    return (
      <div ref={forwardedRef}>
        <input {...props} />
      </div>
    )
  }
)

export default TextField

In the above example users probably DO often want the reference to the <input> and not the <div>, but I think it’s better for us to stay consistent. Instead maybe we can cut down on our use of wrapping elements.

Open questions

I need your help in coming up with solutions for what to do in situations where we are throwing multiple references around. Should we export them all, or just the root?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gui-santoscommented, Mar 24, 2021

We still have the open question to answer. Like the TextField component, the root ref goes to a wrapper div, so should we also export a ref to the input itself?

Yes! We need export refs for the inputs, it’s the main problem in the current solution IMO So I guess in components where there is a wrapper and other components inside of it we will need to export the ref of everyone, no?

1reaction
denkristoffercommented, Mar 23, 2021

We still have the open question to answer. Like the TextField component, the root ref goes to a wrapper div, so should we also export a ref to the input itself?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refs and the DOM - React
When a ref is passed to an element in render , a reference to the node becomes accessible at the current attribute of...
Read more >
UseRef, CreateRef, ForwardRef? What's up with refs in React?
A ref is just a reference to a value, for when you want to store some information in a component, but you don't...
Read more >
A complete guide to React refs - LogRocket Blog
Learn how to use React refs, and why it's important to use them only when React can't handle a function call through its...
Read more >
jsx-eslint/eslint-plugin-react: React-specific linting rules for ...
Use our preset to get reasonable defaults: "extends": [ "eslint:recommended", "plugin:react/recommended" ]. If you are using the new JSX transform from ...
Read more >
Constructors in Functional Components With Hooks
When you're building functional components in React, there's a little feature from class-based components that simply has no out-of-the-box ...
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