简体   繁体   English

React Js:只改变已经点击的表格单元格的class

[英]React Js: Only change the class of the table cell that has been clicked

I'm creating a pathfinding app.我正在创建一个寻路应用程序。 I would like the user to be able to draw its own walls in between points A to B. I only need change the class of each table cell clicked.我希望用户能够在 A 点到 B 点之间绘制自己的墙。我只需要更改每个单击的表格单元格的 class 即可。 However I have only been able to change the class of every table cell, regardless of which table cell is clicked.但是,无论单击哪个表格单元格,我都只能更改每个表格单元格的 class。 I am completely stump on where to go.我完全不知道 go 的位置。 Ideally the user would be able to click and drag across the grid instead of having to click each induvial table cell.理想情况下,用户将能够单击并拖动网格,而不必单击每个单独的表格单元格。 Cheers:)干杯:)

Grid.js网格.js

import React, { Component } from "react";

import Xnode from "./Xnode";
import Ynode from "./Ynode";

export class Grid extends Component {
  constructor(props) {
    super(props);
    this.state = {
      path: true,
      wall: false,
      Xactive: this.state,
      Yactive: this.state,
    };
  }
  
  render() {
    let rows = [];
    let columns = [];

    for (let i = 0; i < 60; i++) {
      columns[i] = (
        <td
          className={this.state.wall ? "wall" : "grid"}
          onClick={() => this.setState({ wall: true })}
          id={i}
          key={i}
        ></td>
      );
    }

    for (let i = 0; i < 25; i++) {
      rows[i] = (
        <tr id={i.toString()} key={i.toString()}>
          {columns}
        </tr>
      );
    }

    return (
      <div>
        <table className="grid">
          <tbody>{rows}</tbody>
        </table>
      </div>
    );
  }
}

export default Grid;

App.js应用程序.js


import './App.css';

import React from 'react';
import {
  BrowserRouter as Router,
  Route,
} from 'react-router-dom';

import './App.css';

import Grid from './components/Grid';

export default () => (
  <Router>
    <div >
        <Route exact path="/" component={Grid} />
</div>
  </Router>
  
);

CSS CSS

 table.grid {
  margin: 80px auto;
  border: 5px solid #333;
  border-collapse: collapse; /* */
}

td.grid {
  width: 15px; height: 15px;
  border: 1.5px solid #333; /* */
}

td.wall {
  width: 15px; height: 15px;
  background-color: black;
  border: 1.5px solid #333; /* */
}

.node-x {
  background-color: mediumaquamarine ;
  border: 2px solid black;
  border-radius: 50%;
  width: 20px;
  height: 20px;
}

.node-y {
  background-color: red ;
  border: 2px solid black;
  border-radius: 50%;
  width: 20px;
  height: 20px;
}

So, every one of your table cells' class is dependent on the state of the grid, and when any table cell is clicked the grid's state changes.因此,每个表格单元格的 class 都取决于网格的 state,当单击任何表格单元格时,网格的 state 会发生变化。 It looks to me like the td elements should be their own component and have their own state as well.在我看来,td 元素应该是它们自己的组件,并且也有自己的 state。 Something like:就像是:

class MyTD extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isClicked: false
    }
  }

  render(){
    <td 
      onClick={() => {this.props.handleClick(); this.setState({isClicked: true});}}
      className={this.state.isClicked ? "wall" : "grid"}
    </td>
  }
}

Then in your Grid component:然后在您的 Grid 组件中:

//for loop
<MyTD key={i} handleClick={() => this.setState({wall: true})}/>

This is assuming you need your child components to change the state of the parent grid.这是假设您需要您的子组件来更改父网格的 state。 If they don't, and the click only needs to change their own state, you don't even need to pass a click handler.如果他们不这样做,并且点击只需要更改自己的 state,您甚至不需要传递点击处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM