gpt4 book ai didi

javascript - 在不同组件中重置 Tic Tac Toe 的方法

转载 作者:行者123 更新时间:2023-12-03 03:03:25 24 4
gpt4 key购买 nike

我正在 React 中创建一个 Tic Tac Toe 板。当有人单击我的应用程序组件中的休息按钮时,我试图让板休息。我正在尝试找出处理这种情况的最佳方法。我一直在阅读有关振奋人心的内容,但如果有更简单的方法,任何建议都会有所帮助。这是我的代码

let data = {
"box":[
[ "", "", "" ],
[ "", "", "" ],
[ "", "", "" ]
],
"players": ['X','O']


}


class DataStore {
constructor(data, players) {
//store that data in the object
this.data = data;
this.players = players;
//empty array for our watchers
//watchers are objects that want to be informed about when the data changes
this.registeredWatchers = [];
}
//add a new watcher to the list
register(watcher) {
this.registeredWatchers.push(watcher);
}
setCrop(newDataState, row, col) {
//update data
this.data[row][col] = newDataState;
//inform all watching objects..
this.registeredWatchers.map((watcher) => {
watcher.dataUpdated();
})
}
}
class Dispatcher {
constructor() {
this.registeredHandlers = []; //D:
}
register(callback) {
this.registeredHandlers.push(callback);
}
dispatch(action) {
//call every method in our registered handlers array
//with this action as an input
this.registeredHandlers.map((handler) => {
//call that individual function in the array..
handler(action);
})
}
}
class BoxComponent extends React.Component {
render() {
return (
<div
className="box"
onClick={this.handleClick.bind(this)}>
<span>{this.props.type}</span>
</div>);
}
handleClick() {
//try a test dispatch
if(this.props.type.length > 0) {
return false;
} else {
boxDispatcher.dispatch({ type: "pick", row: this.props.rowNum, col: this.props.colNum});
}
}
}
class GridComponent extends React.Component {
constructor(props) {
//make sure this stays a react component
super(props);
this.state = {
turn: boxDataStore.players[0],
box: this.props.box
}
//ensure we're listening to the data store
boxDataStore.register(this);
}
dataUpdated() {
this.setState(prevState => ({
box: boxDataStore.data,
// turn: cropDataStore.players,
turn: prevState.turn === 'X'
? boxDataStore.players[1]
: boxDataStore.players[0]
}))
}
render() {
return(
<div>
{
//loop through all the rows...
this.state.box.map((row, rowNum) => {
//and write them out to the page..
return (<div className="row">
{
row.map((cropEntry, colNum) => {
return <BoxComponent type={cropEntry} rowNum={rowNum} colNum={colNum} turn={rowNum}/>
})
}
</div>);
})
}
</div>
)
}
}
class App extends React.Component {
render() {
return (
<div>
<h1>Tic Tac Toe </h1>

<button onClick={this.resetBoard.bind(this)}>reset</button>
<GridComponent box={this.props.data}/>
</div>
);
}
resetBoard() {

}
}
//start of app
var boxDispatcher = new Dispatcher();
var boxDataStore = new DataStore(data.box, data.players);
boxDispatcher.register((action) => {
if(boxDataStore.registeredWatchers["0"].state.turn == "X") {
//actually waint to handle it

boxDataStore.setCrop('X', action.row, action.col);
} else {
boxDataStore.setCrop('O', action.row, action.col);
}

})

最佳答案

如果您正在阅读有关提升状态的内容,您可能已经阅读了 React official documentation 的页面。就说到这了。

There should be a single “source of truth” for any data that changes in a React application. Usually, the state is first added to the component that needs it for rendering. Then, if other components also need it, you can lift it up to their closest common ancestor. Instead of trying to sync the state between different components, you should rely on the top-down data flow.

基本上,如果您不提升状态,则维护单一事实来源会更加困难,因为您必须同步两个状态,但感觉不太对劲。我认为没有“更简单的方法”可以做到这一点,因为它会给您带来很多副作用。

关于javascript - 在不同组件中重置 Tic Tac Toe 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47274050/

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