3

I have an onclick handler:

render() {
  return (
    <div>
      <div className={className} onClick={this.target.bind(this,id)}>{name}</div>
    </div>
  )
}

and here is the function:

target(test, event) {
  event.target.className="addClasss";  
}

I was trying the above way to addClass on event target, however is there a better way to do this? Thanks

3 Answers 3

2

Instead of adding a class manually to a DOM element, you can e.g. add an additional state variable that keeps track of the element that has been clicked. You can then use this in the render method to choose which element that should get the class added to it.

Example

class App extends React.Component {
  state = {
    arr: [{ id: 1, name: "foo" }, { id: 2, name: "bar" }],
    clicked: null
  };

  target(id) {
    this.setState({ clicked: id });
  }

  render() {
    const { arr, clicked } = this.state;

    return (
      <div>
        {arr.map(element => (
          <div
            className={clicked === element.id && "addClass"}
            onClick={this.target.bind(this, element.id)}
          >
            {element.name} {clicked === element.id && "clicked!"}
          </div>
        ))}
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="root"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

so as we can see from the above snippet, when I click on "foo" it renders "clicked" which is correct, however when I click on "bar" the "clicked" text from the "foo" should disappear, but it still stays..any idea how this can be fixed? i.e how can remove the previous set class?
@user1234 I updated the answer to just allow for one clicked item.
0
class App extends React.Component {
  state = {
    clicked: false,
  };

  handleClick = () => {
    this.setState({ clicked: true });
  }

  render() {
    const { clicked } = this.state;
    const className = clicked ? "newClass" : "";
    const name = "add newClass";
    return (
      <div>
        <div className={className} onClick={this.handleClick}>{name}</div>
      </div>
    );
  }
}

1 Comment

so I tried that, it selects both rows and not just the row that is clicked
-1

A little late but for future searchers: use jquery:

target(test, event) {
    var element = $(event.target)
    element.addClass('your class');  
}

OBS: I agree partially with the answers above, react was made to work with states but there are some cases that we don't want to care about controlling every single aspect of our code with states, specially when there are a repetitive class, so I found easier to target these elements with jquery.

2 Comments

Even your code works, I think it's not recommended to use JQuery aside with React (or Vue, or Angular)
React says this is not just the best approach. I didn't find any reason why not to use jQuery. Even React teachs you how to use it reactjs.org/docs/integrating-with-other-libraries.html.

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.