6

Say I have a badge component I want to add sizing for. This is how I currently do it:

import React from 'react';
import Badge from 'react-bootstrap/Badge';
import classNames from 'classnames';

const BadgeExtended = props => {
  const {className, size, ...attr} = props;
  const classes = classNames(
    className,
    size && `badge-${size}`
  );

  return <Badge className={classes} {...attr}>{props.children}</Badge>;
};

export default BadgeExtended;

which works OK. Is that a correct way to do it? Is there a way to extend the original component so that I dind't have to import an extended one, using react-bootstrap/Badge instead?

1
  • i am learning reactjs from couple of week . Even after writting some scratch codes andd few small projects i feel this code is so different from way i write ... Commented Aug 9, 2019 at 16:06

1 Answer 1

2

I think the way you came up with is the right way. If you look at the repository (here), the Badge component is functional, not a class, so there's no way to use the extend keyword.

Making an HOC seems like the best way to achieve what you're doing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.