vue-cli history 本地开发刷新页面丢失_react 开发入门
安装react
直接通过命令安装:npm install -g create-react-app
创建本地服务器
直接通过命令安装:create-react-app first-demo(first-demo为app名称)
启动运行
直接通过命令安装:
cd first-demo
npm start
修改测试
按照提示修改程序app.js文件,修改后页面会自动刷新。
三子棋游戏
初始化
删除src目录下所有文件
复制https://github.com/zacSuo/react-demo/archive/1.0.zip中的index.css和index.js文件
启动查看npm start
增加交互
使用数组将历史数据存储在最上层的game中,可以访问历史步骤,代码如下:(由于格式原因 大家可以复制到自己的编辑器上 但是代码平时一定要自己多敲 尽可能少复制药)
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
function Square(props){
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}
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];
}
}
return null;
}
function getStrByNext(flag){
return flag?'X':'O';
}
class Board extends React.Component {
renderSquare(i) {
return (
<Square
value={this.props.squares[i]}
onClick = {() => this.props.onClick(i)}
/>
);
}
render() {
return (
<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>
);
}
}
class Game extends React.Component {
constructor(){
super();
this.state = {
history:[{
squares:Array(9).fill(null),
}],
xIsNext:true,
currentIdx:0,
};
}
handleClick(i){
const history = this.state.history;
const squares = history[this.state.currentIdx].squares.slice();
const lastSquares = history[history.length - 1].squares;
if(calculateWinner(lastSquares) || lastSquares[i]){
return;
}
squares[i] = getStrByNext(this.state.xIsNext);
this.setState({
history:history.concat([{
squares:squares
}]),
xIsNext: !this.state.xIsNext,
currentIdx: this.state.currentIdx + 1,
});
}
jumpTo(step){
this.setState({
currentIdx:step,
xIsNext:(step % 2)?false:true,
})
}
render() {
const history = this.state.history
const squares = history[this.state.currentIdx].squares;
const winner = calculateWinner(squares);
const moves = history.map((step,move) =>{
const desc = move?'Move #'+move:'Game Start';
return(
<li key={move}>
<a href="#" onClick={() => this.jumpTo(move)} >{desc}</a>
</li>
)
})
let status;
if(winner){
status = 'Winner: ' + winner;
}else{
status = 'Next player: ' + getStrByNext(this.state.xIsNext);
}
return (
<div className="game">
<div className="game-board">
<Board
squares={squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{ status}</div>
<ol>{ moves}</ol>
</div>
</div>
);
}
}
// ========================================
ReactDOM.render(
<Game />,
document.getElementById('root')
);
最终效果
记得关注公众号呦!!!“诗壹锅儿”
原文链接:react 开发入门_真相很简单-CSDN博客_mobx+rn
vue-cli history 本地开发刷新页面丢失_react 开发入门相关推荐
- uniapp打包H5下history模式下刷新页面丢失处理
解决方案:https://www.cnblogs.com/zyfenblog/p/13865523.html
- [vue-cli]在使用vue-cli开发vue项目时,自动刷新页面的原理你了解吗?
[vue-cli]在使用vue-cli开发vue项目时,自动刷新页面的原理你了解吗? 自动刷新页面并不是vue-cli的功能,而是webpack的hot-module-replacement-plug ...
- Vue build打包之后,刷新页面出现404解决方案
Vue build打包之后,刷新页面出现404,HTML5 History 模式 原因分析: vue-router 默认 hash 模式 -- 使用 URL 的 hash 来模拟一个完整的 URL,于 ...
- nginx导致vue设置history模式下的请求丢失参数
nginx导致vue设置history模式下的请求丢失参数 问题描述: 当访问下面的路径时参数会丢失: http://ip/vuecay/path1/path2?name=cay 演示效果: 可以看到 ...
- h5 uniapp history模式下刷新页面404
h5 uniapp history模式下刷新页面404 问题:uniapp 的history 把#去掉了,但是当刷新页面的时候出现404 解决方案 需要服务端支持 如果 URL 匹配不到任何静态资源, ...
- 用友nc65 uap开发刷新按钮二次开发
用友nc65 uap开发刷新按钮二次开发 1.问题现象:卡片刷新,直接返回列表,且定位成第一条,且执行的是全部刷新 2.问题分析: 65向导生成的按钮,刷新按钮使用的同一个 nc.ui.pubapp. ...
- vue 添加完数据后刷新页面_页面刷新vuex数据消失
1.前言 vue构建的项目中,vuex的状态存储是响应式的,当vue组件从store中读取状态的时候,若store中的状态发生变化,那么相应的组件也会得到高效刷新,问题来了,vuex存储的数据只是在页 ...
- vue 添加完数据后刷新页面_vue form 表单提交后刷新页面的方法
最近做的项目中,有增删改表格功能,在操作后需要实时更新页面数据.下面可以这样解决. 1.在methods中 定义好一个初始化渲染实例. 例如 lnitializationData(){//初始化页面数 ...
- vue使用provide / inject 组合刷新页面+单独组件刷新
原文链接:https://www.cnblogs.com/leng12/p/xiaoleng.html 一.this.$router.go(0) 相当于F5刷新,这种方法虽然代码很少,只有一行,但是体 ...
最新文章
- [转]用DateTime.ToString(string format)输出不同格式
- 春季学期第十一周作业
- 卷积神经网络(三):卷积神经网络CNN的简单实现(部分Python源码)
- 转:Linux搭建git私服
- Matlab | MATLAB实现图像的水印去除
- mybatis$和#的区别
- Windows Phone 内容滑动切换实现
- mysql显示百分比例_显示值mysql的百分比
- jumpserver 使用教程_Jumpserver之快速入门
- linux 日志面试题,Linux运维 | 面试题
- maven如果正常配置不成功,就按照我的就可以配置成功了
- Promise实现图片预加载
- Windows 10 Install SQL Server 2005
- 点击微信内网页a标签直接跳转打开淘宝APP的方法实例
- elisa数据处理过程图解_(完整版)ELISA原理和分类(附图解)
- linux客户端连接iscsi,配置ISCSI客户端(LINUX)redhat5-iSCSI-INITIATOR
- 数据结构应用题第三章栈和队列代码c
- js cookie 数组 存读
- 解读趣头条Q4财报:营收同比增长25% 趣头条减速求稳
- 无法确定本地文件类型_如何从文件内容确定文件类型
热门文章
- [paper reading] DenseNet
- matlab2c使用c++实现matlab函数系列教程-randn函数
- jQuery Mobile中链接(包含button和表单提交的链接)的data-*选项
- zynq开发系列2:GPIO连接MIO控制LED闪烁
- PYUIC和PYRRC作为外部工具的配置
- Linux学习13-CentOS安装ab做压力测试
- Apache Camel,Spring Boot 实现文件复制,转移 (转)
- Bootstrap 异常
- Nginx篇--解读nginx配置
- 【视频开发】CximageMat 、CximagelplImage 以及 lplImageMat的转换、像素位深度