0

Basically I have created a button component that looks something like this:

import React from "react";
import styles from "./button.module.scss";

const Button = ({ icon, text, align, bgcolor }) => {
    const btnStyle = {
        justifySelf: align,
    };

    return (
        <>
            <div className={"btn " + bgcolor} style={btnStyle}>
                <i className={icon}></i> {text}
            </div>
        </>
    );
};

export default Button;

In my app I can then call it by:

<Button
    icon="fa-light fa-trash"
    text="Some text"
    align="end"
    bgcolor="yellow"
></Button>

However, at the same time I wish to have an onclick event similar to:

<div onClick={() => setState(!state)}>Click me</div>

How can that be achieved?

1
  • The same way you pass all the other properties. Commented Jan 6, 2021 at 22:08

1 Answer 1

1

Write a function that you can pass as props to the Button component:

const function=()=>{
setState(!state)
}

<Button
    icon="fa-light fa-trash"
    text="Some text"
    align="end"
    bgcolor="yellow"
    function={function}
></Button>

And use it inside like this:

const Button = ({ icon, text, align, bgcolor, function }) => {
    const btnStyle = {
        justifySelf: align,
    };

    return (
        <>
            <div onClick={function} className={"btn " + bgcolor} style={btnStyle}>
                <i className={icon}></i> {text}
            </div>
        </>
    );
};
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.