Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript equivalent of rest/spread props in React stateless component

I am trying to add the following function, taken from bootstrap-react documentation, to my TypeScript + React project:

function FieldGroup({ id, label, help, ...props }) {
    return (
        <FormGroup controlId={id}>
            <ControlLabel>{label}</ControlLabel>
            <FormControl {...props} />
            {help && <HelpBlock>{help}</HelpBlock>}
        </FormGroup>
    );
}

However the rest/spread properties for ECMAScript 6 used as arguments are not supported by TypeScript versions < 2.1.

My current implementation is:

interface FieldGroupProps extends React.HTMLAttributes {
    id?: string;
    label?: string;
    help?: string;
}

class FieldGroup extends React.Component<FieldGroupProps, {}> {
    public render(): JSX.Element {
        const rest = objectWithout(this.props, ["id", "label", "help"]);
        return (
            <FormGroup controlId={this.props.id}>
                <ControlLabel>{this.props.label}</ControlLabel>
                <FormControl {...rest} />
                {this.props.help && <HelpBlock>{this.props.help}</HelpBlock>}
            </FormGroup>
        );
    }
}

Is this functionally (not from a performance perspective) equivalent to the ECMAScript 6 version? If I missed something or it could be made more elegant, what is the recommended way to translate the use of the above rest/spread syntax?

like image 688
Erwin Mayer Avatar asked Oct 30 '16 05:10

Erwin Mayer


People also ask

How do you pass rest props in TypeScript?

const {a, b, c} = props; const htmlProps = rest(props, {a, b, c}); And once TypeScript supports object rest/spread I can just look for all usages of rest() and simplify it to const {a, b, c, ... htmlProps} = props . Save this answer.

Can a stateless component have props?

Stateful and Stateless Components Stateless components, by contrast, have no state. Note that both types of components can use props.

How do you pass props in functional component TypeScript?

To pass a function as props in React TypeScript: Define a type for the function property in the component's interface. Define the function in the parent component. Pass the function as a prop to the child component.

Should I use FC React TypeScript?

You should probably not. The consensus in the React community now seems to be that, you should avoid using React. FC because it causes the component to implicitly take children . This means the component will accept children, even if they're not supposed to.


1 Answers

In TypeScript 3, your first example will work fine so you don't need to rewrite it into a class.

If you like, you can also use your FieldGroupProps interface as well as rewriting it into an arrow function.

const FieldGroup = ({ id, label, help, ...props }: FieldGroupProps) => <FormGroup controlId={id}>
    <ControlLabel>{label}</ControlLabel>
    <FormControl {...props} />
    {help && <HelpBlock>{help}</HelpBlock>}
</FormGroup>;
like image 66
James Monger Avatar answered Sep 25 '22 18:09

James Monger