1

Hi all trying to make a button component that converts to an anchor tag when changing a prop. Im using a switch statement but nothing changes when I change the prop and the onClick is not being handled? Im using NextJS's Link component for the anchor tag

const Button = ({ children }, props) => {
    switch (props.as) {
        case 'link':
            return (
                <Link
                    href={props.link}
                    onClick={props.click}
                    className={styles.hello}>
                    {props.icon && <FontAwesomeIcon icon={props.icon} />}
                    {children}
                </Link>
            );
        case 'button':
            return (
                <button
                    onClick={props.click}
                    type='button'
                    className={styles.btn}>
                    {props.icon && <FontAwesomeIcon icon={props.icon} />}
                    {children}
                </button>
            );
        default:
            return (
                <button
                    onClick={props.click}
                    type='button'
                    className={styles.btn}>
                    {props.icon && <FontAwesomeIcon icon={props.icon} />}
                    {children}
                </button>
            );
    }
};

in my Parent component I'm importing the Button and using it like:

    <Button
                            as='link'
                            icon='coffee'
                            click={props.handleAccept}>
                            RSVP
                        </Button>

In render it appears a button rather than expected anchor tag. Any ideas please?

1
  • 1
    Have you done a console.log(props.as) inside the Button component to verify that the prop's value is what you expect? Commented Oct 25, 2021 at 16:21

2 Answers 2

1
const Button = ({ children, ...props }) => {

It should be like this, props is not second parameter

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

1 Comment

Thanks, that was it.
0

When declaring the Button component, try moving the props inside the destructed object like this and ... in front of it.

const Button = ({ children, ...props }) => {.......};

1 Comment

Thank you you are correct, I think @MCcRist came to the same conclusion I appreciate the input.

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.