Shruti b
Shruti b

Reputation: 11

on button click display react table

I have one react table with subcomponent, in subcomponent, I am having 2 buttons on click of each button need to show react table, please help me with this.

ex:
class Sample extends React{
firstFun=()=>{
return <ReactTable data={} columns={columns}
}
secondFun=()=>{
return <Reacttable data={} columns={columns}
}
subcompo=()=>{
// some code
<bt1 onclick={this.firstFun}/>
<bt2 onclick={this.secondFun}/>
}
render(){
return(
<ReactTable
data={}
submcomponent={this.subcompo}
/>
)}
}

Upvotes: 1

Views: 1863

Answers (1)

gergana
gergana

Reputation: 691

You can set in state which button is clicked and active with boolean value and display different tables. Is something like this similar to what you are looking for:

class Sample extends React {
    state = {
        firstButtonActive: false,
        secondButtonActive: false
    }
    handleFirstButtonClick = () => {
        this.setState({ firstButtonActive: !this.state.firstButtonActive})
    }
    handleSecondButtonClick = () => {
        this.setState({ secondButtonActive: !this.state.secondButtonActive })
    }
    subcompo = () => {
        // some code
        <bt1 onclick={this.handleFirstButtonClick} />
        <bt2 onclick={this.handleSecondButtonClick} />
    } 

    render() {
        const { firstButtonActive, secondButtonActive } = this.state;
        return (
            <>
                <ReactTable
                    data={}
                    submcomponent={this.subcompo}
                />

                {firstButtonActive && <ReactTable data={} columns={columns}/>}

                {secondButtonActive && <ReactTable data={} columns={columns} />}
            </>
        )
    }
}

Upvotes: 2

Related Questions