Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass props to React Big Calendar custom components?

I'm using React Big Calendar with custom event components.

In my custom component, I need to display a few buttons which a user can click (via popover). I got the popup thing working fine, but I also want the class that renders BigCalendar to get notified when a button in the popover is clicked. How do we pass an "onButtonClick" event to my custom component as props? Here's a simplified version of my code

class Parent extends Component {

    popoverButtonClickHandler = (e) => {
        //handle button click
    }

    render() {
        return (
            <BigCalendar
                ...
                events={myEvents}
                components={{
                    event: CustomEvent
                }}
            />
        );
    }
}

And here's my CustomEvent class

class CustomEvent extends Component {
    render() {
        return (
            <div>
                <p>My event title: {this.props.title}</p>
                <MyPopover>
                    <Button onClick={this.props.onPopoverButtonClick}>
                </MyPopover>
            </div>
        )
    }
}

I'm trying to figure out how I can pass

onPopoverButtonClick={this.popoverButtonClickHandler}

to my CustomEvent so that Parent will be notified when the button is clicked.

Any ideas how I can achieve this? Thanks

like image 809
Yoope Avatar asked Oct 19 '25 05:10

Yoope


1 Answers

OK, I managed to get this working after looking at the here on GitHub

class Parent extends Component {

    popoverButtonClickHandler = (e) => {
        //handle button click
    }

    render() {
        return (
            <BigCalendar
                ...
                events={myEvents}
                components={{
                    event: CustomEventContainer({
                        onPopoverButtonClick: this.popoverButtonClickHandler
                    })
                }}
            />
        );
    }
}

And here's the CustomEventContainer and CustomEvent

const CustomEventContainer = ({ onPopoverButtonClick }) => props => {
    return <CustomEvent event={props} onPopoverButtonClick={onPopoverButtonClick} />;
}

const CustomEvent = React.memo((props) => {

    //...
    return (
        <div>
            <Button content='View' onClick={(e) => props.onPopoverButtonClick(e)} />
        </div>
    );
})
like image 163
Yoope Avatar answered Oct 20 '25 18:10

Yoope