score:2

Accepted answer

You can maintain a selectedRow in the state and add a class name to the row based on matching index.

className={this.state.selectedRow === i ? "tableSelected" : "" }

Full working code below

class App extends React.Component {
  state = {
    selectedRow: -1
  };

  render() {
    return (
      <table>
        <thead>
          <tr>
            <th>name</th>
            <th>age</th>
            <th>address</th>
          </tr>
        </thead>
        <tbody className="tableHover">
          {this.props.students.map((item, i) => {
            return (
              <tr key={i} onClick={this.changeColor(i)} className={this.state.selectedRow === i ? "tableSelected" : "" }>
                <td>{item.name}</td>
                <td>{item.age}</td>
                <td>{item.address}</td>
              </tr>
            );
          })}
        </tbody>
      </table>
    );
  }

  changeColor = selectedRow => e => {
    if (selectedRow !== undefined) {
      this.setState({ selectedRow  });
    }
  };
}

ReactDOM.render(<App students={[{name: "a"}, {name: "b"}]}/>, document.getElementById("app"));
.tableHover :hover {
  color: white;
  background-color: blue;
}

.tableSelected {
  background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>

score:1

You can just set the index to your state and if the index equals what is set then add your color like so:

class YourComponent extends Component {
  state = {
    isActive: null
  };

  toggleActive = i => {
    //Remove the if statement if you don't want to unselect an already selected item
    if (i === this.state.isActive) {
      this.setState({
        isActive: null
      });
    } else {
      this.setState({
        isActive: i
      });
    }
  };

  render() {
    return (
      <Table>
        <thead>
          <tr>
            <th>name</th>
            <th>age</th>
            <th>address</th>
          </tr>
        </thead>
        <tbody className="tableHover">
          {this.props.students.map((item, i) => {
            return (
              <tr
                style={
                  this.state.isActive === i
                    ? { background: 'green' }
                    : { background: 'yellow' }
                }
                key={i}
                onClick={() => this.toggleActive(i)}
              >
                <td>{item.name}</td>
                <td>{item.age}</td>
                <td>{item.address}</td>
              </tr>
            );
          })}
        </tbody>
      </Table>
    );
  }
}

Related Query

More Query from same tag