A
A
Anatoly Kuchin2020-06-26 07:58:01
React
Anatoly Kuchin, 2020-06-26 07:58:01

How can I change the background color of only the selected button, and not all of them, when the button is clicked?

I remove the block by key. How can I change the background color of only the selected button, and not all of them, when the button is clicked?

let btn = this.state.change ? "clinic-card__bookmark" : "clinic-card__bookmark-active";

        function handleClick(e) {
            e.preventDefault();
        }

        return <div className="search-result-list" key={clinic.ID}>
            <div className="search-result__card card-shadow">
                <div className="clinic-card">
                    <h2 className="clinic-card__title">
                        {clinic.NAME}
                        <div className={btn_class} onClick={handleClick}>.....

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Loli E1ON, 2020-06-26
@devil40rus

const Button = () => {
  const [state, setState] = useState(false);

  const onClickButton = () => setState(state => !state);

  return (
    <div className={state ? "btn-active" : "btn"} onClick={onClickButton}>
      Button
    </div>
  );
};

const Buttons = () => {
  return (
    <div>
      <Button />
      <Button />
      <Button />
    </div>
  );
};

const App = () => <Buttons />;

ReactDOM.render(<App/>, document.getElementById("root"));

H
hzzzzl, 2020-06-26
@hzzzzl

save somewhere that this particular clinic.ID is currently -active

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question