1

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}
/>
)}
}
1
  • 3
    Come on, indent your code please. Commented Nov 29, 2019 at 16:14

1 Answer 1

2

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} />}
            </>
        )
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Check again if your functions on buttons click are the same name. I can't help more, I am not sure what you are trying to achieve and how subcomponent is rendered in your React Table. I am not sure if this is the right way for rendering sub components. Have you checked this example ? codesandbox.io/s/pwxw5jjl9j?from-embed
suspect data={} .. Share a link on codesandbox that way you might get the issue resolved quicker

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.