Skip to content

Feature Request: Allow Fragments to accept the className prop #12860

@bruffstar

Description

@bruffstar

Use case:

A set of elements should be wrapped in a <div> (with a class), but only depending on certain scenarios.

Example:

Test Component
import * as React from 'react';

class Test extends React.Component {
    render() {
        const Wrapper = this.props.wrap ? 'div' : React.Fragment;

        return (
            <Wrapper className={'wrapper'}>
               <div>Child 1</div>
               <div>Child 2</div>
            </Wrapper>
        );
    }
}

export default Test;
Scenario 1
<Test />
<div>Child 1</div>
<div>Child 2</div>
Scenario 2
<Test wrap={true} />
<div class="wrapper">
    <div>Child 1</div>
    <div>Child 2</div>
</div>

Even though the above examples will actually output the way they are intended, an error is still being thrown:

image

Solution:

Allow the className prop on Fragments, but simply ignore it, don't do anything with it.

Of course there are many other way this can be achieved without using Fragments, but I think this approach is really clean.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions