關(guān)于反應(yīng)堆的小問題。所以我是新反應(yīng),我正在嘗試做一個(gè)井字游戲。
目前,這是我所擁有的: App.js:
import logo from './logo.svg';
import './App.css';
import { Row } from './components/Row';
import {Board} from './components/Board'
function App() {
// App renders Row
return (
<div>
<Board />
</div>
);
}
export default App;
Board.js
import React from "react";
import { ReactDOM } from "react";
import { Row } from "./Row";
export function Board() {
return (
<div className="Board">
<Row />
<Row />
<Row />
</div>
)
}
Row.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Square } from './Square';
export function Row() {
return(
<div className='Row'>
<Square />
<Square />
<Square />
</div>
)
}
廣場. js
import React, {useState} from 'react';
import ReactDOM from 'react-dom';
export function Square(props){
// Square is rendered by App
const [currLetter, changeLetter] = useState(" ");
function toggleLetter(str) {
if(str === " ") {return "X";}
else if(str === "X") {return "O";}
else if(str === "O") {return "X";}
}
return(
<div >
<button onClick={() => changeLetter(toggleLetter)} className='Square'> {currLetter} </button>
</div>
);
}
它運(yùn)行得很好,但是我想知道為什么當(dāng)我像這樣寫Square.js時(shí)它不能運(yùn)行:
export function Square(props){
// Square is rendered by App
const [currLetter, changeLetter] = useState("X");
function toggleLetter(str) {
if(str === "X") {changeLetter("O");}
else if(str === "O") {changeLetter("X");}
}
return(
<div >
<button onClick={toggleLetter} className='Square'> {currLetter} </button>
</div>
);
}
為什么onClick不能直接接受toggleLetter?這是一個(gè)函數(shù),對嗎?
第二題 這是我的css的樣子:
.Board {
display: flex;
flex-direction: column;
justify-content: space-between;
/* width: 55%;
height: 55%; */
margin-top: 18%;
}
.Row {
text-align: center;
/* position: absolute; */
/* left: 35%;
right: 50%;
top: 25%;
width: 25%;
height: 25%; */
display: flex;
flex-direction: row;
justify-content: center;
}
.Square {
/* position: absolute; */
width: 70px;
height: 70px;
border-color: #99ccff;
text-align: center;
font-size: xx-large;
font-family: 'Courier New', Courier, monospace;
}
/* .Row {
} */
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
body {
min-height: 100%;
margin: 0;
padding: 0;
background-color: #ffcccc;
}
html {
height: 100%;
}
我想做的是確保板子在正文中居中,因此在頁面上居中。實(shí)際上,它位于頁面的中心,但隨后正文被向下移動(dòng)。當(dāng)我檢查頁面時(shí),正文的上邊緣被向下移動(dòng)到與紙板的上邊緣相同的高度,而不是默認(rèn)情況下正文在哪里,紙板在它的中心。我該如何解決這個(gè)問題?
它看起來像什么:
怎么樣了
所以,你有一個(gè)這樣的應(yīng)用組件:
<div>
<Board />
</div>
向這個(gè)DIV添加一個(gè)類名,類似于:
<div className="myClass">
<Board />
</div>
然后在css中添加以下屬性:
.myClass {
display:flex;
height: 100%;
align-items: center;
justify-content: center;
}
就這樣。;)