我尝试使用路线渲染游戏组件。在单击链接(在app.js中定义)时,它会指向所需的页面(游戏),但不会渲染组件游戏。我试图找到答案,但没有一个帮助。我反应很新。请帮忙!!谢谢!! 以下是代码段:
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App.js';
import Game from './Game.js';
import registerServiceWorker from './registerServiceWorker';
import {
HashRouter,
Route,
Link,
BrowserRouter as Router
} from 'react-router-dom';
ReactDOM.render(
<Router>
<Route exact path = "/" component={App}>
<Route path="game" component={Game}></Route>
</Route>
</Router >,
document.getElementById('root')
);
App.js
import React, { Component } from 'react';
import './App.css';
import { Link } from 'react-router-dom'
class App extends Component {
render() {
return (
<div>
<Link to= "game">Go</Link>
{this.props.children}
</div>
);
}
}
export default App;
Game.js
import React from 'react';
import './index.css';
import registerServiceWorker from './registerServiceWorker';
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}
export class Board extends React.Component {
constructor(props) {
super(props);
this.state = {
squares: Array(9).fill(null),
xIsNext: true,
};
}
handleClick(i) {
const squares = this.state.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? 'X' : 'O';
this.setState({
squares: squares,
xIsNext: !this.state.xIsNext,
});
}
renderSquare(i) {
return (
<Square
value={this.state.squares[i]}
onClick={() => this.handleClick(i)}
/>
);
}
render() {
const winner = calculateWinner(this.state.squares);
let status;
if (winner && winner != 'draw') {
status = 'Winner: ' + winner;
}
else if (winner && winner === 'draw'){
status = "Both won!! It's a tie!! ";
}
else {
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
}
return (
<div>
<div className="status">{status}</div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
export default class Game extends React.Component {
render() {
return (
<div className="game">
<div className="game-board">
<Board />
</div>
</div>
);
}
}
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
else if(!squares.includes(null)) {
return 'draw';
}
}
return null;
}
registerServiceWorker();
答案 0 :(得分:0)
你为什么要包裹一条路线?
试试这个:
<Route exact path="/" component={App} />
<Route path="/game" component={Game} />
路由应该被视为兄弟姐妹,它们也按照它们的写入顺序工作。
有关详细信息,请查看此页面:https://reacttraining.com/react-router/web/example/basic
EDIT路由器示例: 只需用这个替换你的路由器。
<Router>
<div>
<Route exact path="/" component={App} />
<Route path="/game" component={Game} />
</div>
</Router>