Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use props on React.Fragment?

I know that a React Fragment can be used to group child components like this:

render() {
  return (
    <React.Fragment>
      <Foo />
      <Bar />
      <Baz />
    </React.Fragment>
  );
}

But can you add props to it? Say you want to use the spread operator:

<React.Fragment {...otherProps}>
  ...
</React.Fragment>
like image 604
Paul Razvan Berg Avatar asked Dec 07 '19 11:12

Paul Razvan Berg


People also ask

Can you style React fragment?

If you need to style a Fragment then you probably shouldn't be using it in the first place. Fragments exists as a workaround to return adjacent JSX elements, they don't render anything to the DOM so they can't be stylized.

Can you add a key to a fragment?

Fragments can have key props! To specify a key for a fragment, you'll need to use the standard JSX element syntax; you can't use the new <></> syntax. Why would you want to give a Fragment a key? Well, it allows fragments to be used as items in arrays.

Do React fragments need keys?

We no longer need to provide keys, we don't need to add array commas, and we still avoid adding an extraneous DOM element because React. Fragment doesn't become an actual element during rendering. We can import Fragment to avoid having to fully write out React.

Why we use React fragment instead of DIV?

Therefore, React fragments are better than the 'div' tags. With React Fragment, you can render multiple elements of a component without adding extra div tags. We can write cleaner, more readable code with React Fragments. It takes up less memory and renders components faster. Each component is rendered as expected.

How to add a key prop to a react fragment?

Use the more verbose syntax of fragments to add a key prop to a React fragment, e.g. <React.Fragment key= {key}>. The more verbose syntax achieves the same result - groups a list of elements without adding extra nodes to the DOM. We used the more verbose syntax for React fragments to be able to add a key prop to the fragment.

When should we use React fragment?

We should use React Fragment when we want to group a list of children nodes without adding any extra nodes. Unlike a typical div, a fragment is not rendered on the DOM. This basically means that we can use React.Fragment where we usually use a div.

Why do we use more verbose syntax for react fragments?

We used the more verbose syntax for React fragments to be able to add a key prop to the fragment. Fragments are used when we need to group a list of children without adding extra nodes to the DOM. If you use the shorthand syntax for fragments <> </>, you won't be able to pass any props to the fragment.

Do react fragments slow down adoption?

After using fragments for a while, you may run into an eensy-weensy issue: typing out <React.Fragment> is bothersome compared to typing <div>. And while this won’t get in the way when fragments are truly necessary, it certainly can slow down adoption. Which is why, I suppose, fragments also come with a concise new syntax:


1 Answers

No, you can't. As per the React docs on Fragments:

key is the only attribute that can be passed to Fragment. In the future, we may add support for additional attributes, such as event handlers.

Therefore, if you want to add props to a wrapper component, switch to a good ol' div.

like image 50
Paul Razvan Berg Avatar answered Sep 23 '22 15:09

Paul Razvan Berg