Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div id="app"></div>
<script src="https://unpkg.com/react@16.4.0/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16.4.0/umd/react-dom.production.min.js"></script>
</body>
</html>
 
console.clear();
const users = [
  {id: 1, name: 'A'},
  {id: 2, name: 'B'},
  {id: 3, name: 'C'},
  {id: 4, name: 'D'},
];
class TableRow extends React.PureComponent {
  defaultProps = {
    selected: false
  }
  render() {
    const { selected, id, name, handleSelect } = this.props;
    console.log(`render TableRow :: ${id} :: ${name}`);
    return (
      <tr>
        <td><input name={id} type="checkbox" checked={selected} onChange={handleSelect} /></td>
        <td>{id}</td>
        <td>{name}</td>
      </tr>
    );
  }
}
class Table extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selected: {},
    }
  }
  handleSelect = (e) => {
    const selected = this.state.selected;
    selected[e.target.name] = e.target.checked;
    this.setState({ selected });
  }
  render() {
    return (
      <table>
        <thead>
          <tr>
            <th />
            <th>ID</th>
            <th>Name</th>
          </tr>
        </thead>
        <tbody>
          {
            users.map((user) => {
              return <TableRow key={user.id} id={user.id} name={user.name} selected={this.state.selected[user.id]} handleSelect={this.handleSelect} />;
            })
          }
        </tbody>
      </table>
    );
  }
}
const App = () => {
  return <Table />
}
ReactDOM.render(<App />, document.getElementById('app'));
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
revathskumarpro
0viewers