N
N
Nikita2018-07-17 20:54:46
JavaScript
Nikita, 2018-07-17 20:54:46

How to change styles based on state in React?

I use styled-components and react, when you click on the button, you need to change its styles, here is an example https://codesandbox.io/s/ry52xp992m.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-07-18
@fataliity

render() {
  const { isSelected } = this.state;
  const content = isSelected ? '1' : '0';

  return (
    <App>
      <Button
        onClick={this.handleClick}
        isSelected={isSelected}
      >
        Button
      </Button>
      <p>{content}</p>
    </App>
  );
}

In the example, div.App is replaced by StyledComponent App

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question