2

I have this structure in app, each level is a component:

Assigning
├── Assignee (key='user0')
│   ├── AssigneeTask (key='user0task0')
|   |   └── button (onClick)
│   ├── AssigneeTask (key='user0task1')
|   |   └── button (onClick)
|   └── AssigneeTask (key='user0task2')
|       └── button (onClick)
├── Assignee (key='user1')
└── Assignee (key='user2')

I want to remove Assignee component when i click on button in it using the key of component, how can i do this in reactjs? I tried to pass the key to the onClick and set the display to none, but i can't get the component by its key!

10
  • Can you post what you have tried? Commented Jul 23, 2018 at 11:53
  • see updates.... Commented Jul 23, 2018 at 11:55
  • This depends a bit on your implementation. Can you show the code for Assigning? Commented Jul 23, 2018 at 11:59
  • Use refs. That would solve your problem. Also, you don't really need to access key here. Keys are only used by react internally to identify each node individually. We as a react dev don't really use key per se. Commented Jul 23, 2018 at 12:00
  • @UtkarshPramodGupta there is a reason react gives a warning if you are missing access keys Commented Jul 23, 2018 at 12:17

1 Answer 1

2

You can use state / function uplifting:

class Container extends Component {
    [...]
    render() {
        return this.state.opened && <ContainerButton clickedButton={this.close} />
    }

    close = () => {
        this.setState({opened: false});
    }
}

class ContainerButton extends Component {
    [...]

    render() {
        <button onClick={this.props.clickedButton} />
    }
}

If you now trigger the onClick on the button, the close function of Container gets triggered

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.