最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add styles to React.Fragment - Stack Overflow

programmeradmin4浏览0评论

Im working with React and my ponents follows this files structure schema:

- ponentName
  |- ponentName.tsx
  |- ponentName.scss

There are some of these ponents that are wrap with a <React.Fragment> like that:

render() {
  return (
    <React.Fragment>
      <ChildA />
      <ChildB />
      <ChildC />
    </React.Fragment>
  );
}

Is there a way to select this element in css ? Something like:

React.Fragment {
  padding-top: 30px;
}

Because it's not possible to add an id or a className to it. Any idea ?

Im working with React and my ponents follows this files structure schema:

- ponentName
  |- ponentName.tsx
  |- ponentName.scss

There are some of these ponents that are wrap with a <React.Fragment> like that:

render() {
  return (
    <React.Fragment>
      <ChildA />
      <ChildB />
      <ChildC />
    </React.Fragment>
  );
}

Is there a way to select this element in css ? Something like:

React.Fragment {
  padding-top: 30px;
}

Because it's not possible to add an id or a className to it. Any idea ?

Share Improve this question asked Mar 5, 2020 at 22:13 johannchopinjohannchopin 14.9k11 gold badges63 silver badges121 bronze badges 3
  • 4 Fragments let you group a list of children without adding extra nodes to the DOM. So, there is no element in DOM, nothing to style. reactjs/docs/fragments.html – m1k1o Commented Mar 5, 2020 at 22:17
  • 1 Fragment doesn't appear in the DOM. It's basically an escape hatch that allows you to return an array of ponents from a ponent, so no you can't style it because it technically doesnt exist. – JMadelaine Commented Mar 5, 2020 at 22:18
  • 1 The point of a fragment is that it won't render anything. So no you would not be able to select it like that. If you need to do something like this why not just use a div? – Brian Thompson Commented Mar 5, 2020 at 22:18
Add a ment  | 

1 Answer 1

Reset to default 6

As others have pointed out in ments, React.Fragment does not add any extra nodes to the DOM. But in your case, it seems like you need to add a parent node. You can either add padding to ChildA or replace React.Fragment with a div. With the latter, I'd be mindful of any styling that expects 3 elements (with React.Fragment) and instead receives one element (div).

发布评论

评论列表(0)

  1. 暂无评论