gpt4 book ai didi

css - React - 使用三元在功能组件中应用 CSS 类

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:05 26 4
gpt4 key购买 nike

我是 React 的新手,正在开发 John Conway - Game of Life 应用程序。我为游戏板本身构建了一个 Gameboard.js 功能组件(它是 App.js 的子组件)和一个 Square.js 功能组件组件,代表棋盘中的单个方 block (是 Gameboard 的子项和 App 的孙项)。

App 中,我有一个名为 alive 的函数,我想在用户单击它时更改单个方 block 的颜色。 App 还有一个“alive”属性,其状态最初设置为 false,alive 会在调用时将该属性更改为 true。

这是App.js:

import React, { Component } from 'react';
import './App.css';
import GameBoard from './GameBoard.js';
import Controls from './Controls.js';

class App extends Component {
constructor(props){
super(props);

this.state = {
boardHeight: 50,
boardWidth: 30,
iterations: 10,
reset: false,
alive: false
};
}

selectBoardSize = (width, height) => {
this.setState({
boardHeight: height,
boardWidth: width
});
}

onReset = () => {

}

alive = () => {
this.setState({ alive: !this.state.alive });
console.log('Alive function has been called');

}



render() {
return (
<div className="container">
<h1>Conway's Game of Life</h1>

<GameBoard
height={this.state.boardHeight}
width={this.state.boardWidth}
alive={this.alive}
/>

<Controls
selectBoardSize={this.selectBoardSize}
iterations={this.state.iterations}
onReset={this.onReset}
/>

</div>
);
}
}

export default App;

Gameboard 看起来像这样并将 props.alive 传递给 Square:

import React, { Component } from 'react';
import Square from './Square.js';

const GameBoard = (props) => {
return (
<div>
<table className="game-board">
<tbody>
{Array(props.height).fill(1).map((el, i) => {
return (
<tr key={i}>
{Array(props.width).fill(1).map((el, j) => {
return (
<Square key={j} alive={props.alive}/>
);
})}
</tr>
);
})}
</tbody>
</table>
</div>
);
}

export default GameBoard;

在我的 CSS 中,我有一个名为 active 的类,如果单击它,它会更改单个方 block 的颜色。我怎样才能在 Square 中单击 td 元素时颜色发生变化(即 CSS 类变为事件状态)?

我已经试过了:

import React, { Component } from 'react';

const Square = (props) => {

return(
<td className={props.alive ? "active" : "inactive"} onClick={() => props.alive()}></td>
);
}

export default Square;

CSS 看起来像这样:

.Square {
background-color: #013243; //#24252a;
height: 12px;
width: 12px;
border: .1px solid rgba(236, 236, 236, .5);
overflow: none;

&:hover {
background-color: #48dbfb; //#00e640; //#2ecc71; //#39FF14;
}
}

.inactive {
background-color: #013243; //#24252a;
}

.active {
background-color: #48dbfb;
}

我怎样才能使 .Square CSS 类始终应用于每个正方形,但如果它处于事件状态,则单个正方形颜色会发生变化?换句话说,我能否将 Square 的 td 设置为始终使用 .Square CSS 类设置样式,然后 Square 中的各个元素可以根据是否适当着色aliveApp 的状态下是真的吗?

是否有三元方法总是设置一个特定的 CSS 类,然后另外设置 2 个其他类中的 1 个....即Square CSS 类始终显示并根据逻辑/状态呈现事件或非事件?

最佳答案

评论有理有据

你可以使用 template literal并在其中嵌入三元条件:

return (
<td
className={`Square ${props.alive ? "active" : "inactive"}`}
onClick={() => props.alive()}
></td>
);

快速复习模板文字:使用反引号包裹字符串,您可以通过将其包裹在 ${} 模式中来在其中插入 JavaScript 表达式。作为奖励,模板文字可以跨越多行,因此不再有尴尬的字符串连接!

const myName = "Abraham Lincoln";
const myString = `Some text.
This text is on the next line but still in the literal.
Newlines are just fine.
Hello, my name is ${myName}.`;

编辑:我现在看到的更大问题是您没有在任何地方存储每个单元格的状态。您只有一个 bool 值存储在 App 中,称为 alive...您真正需要的是 bool 值的数组,每个 bool 值代表单个 Square 的状态。

“事件”状态数组应该存在于 AppGameBoard 中,遵循 "the data flows down" 的 React 原则.在您的情况下,您可以尝试将其保留在 App 中,这样 GameBoardSquare 就可以保留纯功能组件。

App 中,您可以在构造函数中创建一个新的二维数组 board,并用 0 的子数组填充它> 初始值:

// App.js
constructor(props){
super(props);

this.state = {
boardHeight: 50,
boardWidth: 30,
board: [],
iterations: 10,
reset: false,
};

this.state.board = new Array(this.state.boardHeight).fill(new Array(this.state.boardWidth).fill(0));
}

board 数组中,每个索引代表一行。因此,[[0, 0, 1], [0, 1, 0], [1, 1, 1]] 的简化示例将表示:

0 0 1
0 1 0
1 1 1

GameBoard 应该完全基于传递给它的 board 属性渲染你的单元格网格,并传递每个 Square 它的事件值和回调用作 Prop :

const GameBoard = (props) => {
return (
<div>
<table className="game-board">
<tbody>
{this.props.board.map((row, y) => {
return <tr key={y}>
{row.map((ea, x) => {
return (
<Square
key={x}
x={x}
y={y}
isAlive={ea}
aliveCallback={this.props.alive}
/>
);
})}
</tr>;
})}
</tbody>
</table>
</div>
);
}

从那里您应该能够看到这个应用程序是如何工作的。 App 存储游戏状态并呈现功能组件 GameBoardGameBoard中,每个Square根据其alive值进行渲染,点击时触发aliveCallbackaliveCallback 应该在 Appboard 数组中根据其 xy 属性。

关于css - React - 使用三元在功能组件中应用 CSS 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52413319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com