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.

New rule suggestion: prefer-fragment

See original GitHub issue

Any time bare <div>s with no attributes are used, a fragment <>...</> should be used instead.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jkilliancommented, May 15, 2018

Hmm, I’m a little concerned with this rule, I’m not sure it will be generally applicable. Having a bare div is semantically quite different than a fragment, for example a <div>:

  • creates an HTML element with display: block whereas a fragment does not
  • is stylable via CSS and may have CSS rules targeting it

I’m also curious which of the following elements the rule would affect in the hypothetical situation below:

public render() {
  return <header>
    <div>Option 1</div>
    <div><p>Option 2</p></div>
    <div />
  </header>;
}
1reaction
aoberoicommented, Oct 2, 2018

I had a related idea: what if there was an option for jsx-wrap-multiline that enforced the preference of wrapping with a fragment, rather than parenthesis.

I currently have code like this:

const Foo = () => <>
  <h2>I'm a Foo</h2>
  <p>More content here</o>
</>;

I’m getting errors for the jsx-wrap-multiline rule, but I think in this case the fragment serves the purpose of the parenthesis. When there’s only one element parent in the multiline jsx, I would still want the error.

I know this isn’t exactly the rule being described here, but I think this is useful in determining what exactly the rules are trying to enforce.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rule suggestion: consistent usage of <React.Fragment> vs ...
Instead of preferring React.Fragment over <>, I think that it could be valuable to be enforce the usage of either one for consistency....
Read more >
Best practice for instantiating a new Android Fragment
Best practice to instance fragments with arguments in android is to have static factory method in your fragment.
Read more >
Communicating with fragments - Android Developers
To reuse fragments, build each as a completely self-contained component that defines its own layout and behavior.
Read more >
Breaking Grammar Rules: Sentence Fragments - A.J. Hartley
Fragments look like sentences but aren't. They can be short, but don't have to be, while sentences can be short without being fragments....
Read more >
fragment rule 'x' contains an action or command which can ...
Hi! I know the topic of actions in fragments have been discussed before, here and in a couple of issues on github.
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