React学习——快速上手

文章目录

初步

https://php.cn/faq/400956.html

1、可以手动使用npm来安装各种插件,来从头到尾自己搭建环境。

如:

bash 复制代码
npm install react react-dom --save
npm install babel babel-loader babel-core babel-preset-es2015 babel-preset-react --save
npm install babel webpack webpack-dev-server -g

2、脚手架

create-react-app

bash 复制代码
npm install -g create-react-app
bash 复制代码
create-react-app my-app
cd my-app/
npm start

模块思维

https://react.dev/learn/tutorial-tic-tac-toe

官方文档的井字游戏案例

js 复制代码
import { useState } from 'react';

function Square({ value, onSquareClick }) {
  return (
    <button className="square" onClick={onSquareClick}>
      {value}
    </button>
  );
}

function Board({ xIsNext, squares, onPlay }) {
  function handleClick(i) {
    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    const nextSquares = squares.slice();
    if (xIsNext) {
      nextSquares[i] = 'X';
    } else {
      nextSquares[i] = 'O';
    }
    onPlay(nextSquares);
  }

  const winner = calculateWinner(squares);
  let status;
  if (winner) {
    status = 'Winner: ' + winner;
  } else {
    status = 'Next player: ' + (xIsNext ? 'X' : 'O');
  }

  return (
    <>
      <div className="status">{status}</div>
      <div className="board-row">
        <Square value={squares[0]} onSquareClick={() => handleClick(0)} />
        <Square value={squares[1]} onSquareClick={() => handleClick(1)} />
        <Square value={squares[2]} onSquareClick={() => handleClick(2)} />
      </div>
      <div className="board-row">
        <Square value={squares[3]} onSquareClick={() => handleClick(3)} />
        <Square value={squares[4]} onSquareClick={() => handleClick(4)} />
        <Square value={squares[5]} onSquareClick={() => handleClick(5)} />
      </div>
      <div className="board-row">
        <Square value={squares[6]} onSquareClick={() => handleClick(6)} />
        <Square value={squares[7]} onSquareClick={() => handleClick(7)} />
        <Square value={squares[8]} onSquareClick={() => handleClick(8)} />
      </div>
    </>
  );
}

export default function Game() {
  const [history, setHistory] = useState([Array(9).fill(null)]);
  const [currentMove, setCurrentMove] = useState(0);
  const xIsNext = currentMove % 2 === 0;
  const currentSquares = history[currentMove];

  function handlePlay(nextSquares) {
    const nextHistory = [...history.slice(0, currentMove + 1), nextSquares];
    setHistory(nextHistory);
    setCurrentMove(nextHistory.length - 1);
  }

  function jumpTo(nextMove) {
    setCurrentMove(nextMove);
  }

  const moves = history.map((squares, move) => {
    let description;
    if (move > 0) {
      description = 'Go to move #' + move;
    } else {
      description = 'Go to game start';
    }
    return (
      <li key={move}>
        <button onClick={() => jumpTo(move)}>{description}</button>
      </li>
    );
  });

  return (
    <div className="game">
      <div className="game-board">
        <Board xIsNext={xIsNext} squares={currentSquares} onPlay={handlePlay} />
      </div>
      <div className="game-info">
        <ol>{moves}</ol>
      </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];
    }
  }
  return null;
}

1、分解组件
2、构建静态版本

可以"自上而下"地构建组件,从层次结构中较高的组件开始构建,也可以"自下而上"地从较低的组件开始构建。在更简单的例子中,自上而下通常更容易,而在较大的项目中,自下而上更容易。

特点:单向数据流,数据从顶级组件向向树底部的组件。

3、查找 UI 状态的最小但完整的表示形式

找出应用程序所需状态的绝对最小表示形式,并按需计算其他所有内容。

哪些是状态?

会随着时间的推移保持不变吗?如果是,则不是状态。

是通过 props 从父级传入的吗?如果是,则不是状态。

能根据组件中的现有状态或道具来计算它吗?如果是,那绝对不是状态!

4、确定state的位置

确定应用的最小状态数据后,需要确定哪个组件负责更改此状态,或者哪个组件拥有该状态。

React 使用**单向数据流,**将数据从父组件向下传递到子组件。

对于应用程序的每个状态:

1、确定根据该状态呈现某些内容的每个组件

2、查找最接近的公共父组件

3、决定state在哪

1、通常可以放入公共父级中

2、公共父级上方的某个组件中

3、如果找不到适合拥有状态的组件,创建一个仅用于保存状态的新组件,并将其添加到公共父组件上方的层次结构中的某个位置

5、添加反向数据流

添加父组件到子组件的方法,在父组件更新数据

相关推荐
进阶小白猿9 分钟前
Java技术八股学习Day29
学习
CHU7290359 分钟前
一番赏盲盒抽卡机小程序:解锁惊喜体验与社交乐趣的多元功能设计
前端·小程序·php
RFCEO10 分钟前
前端编程 课程十二、:CSS 基础应用 Flex 布局
前端·css·flex 布局·css3原生自带的布局模块·flexible box·弹性盒布局·垂直居中困难
闫记康15 分钟前
linux配置ssh
linux·运维·服务器·学习·ssh
浅念-31 分钟前
C语言——双向链表
c语言·数据结构·c++·笔记·学习·算法·链表
天若有情67334 分钟前
XiangJsonCraft v1.2.0重大更新解读:本地配置优先+全量容错,JSON解耦开发体验再升级
前端·javascript·npm·json·xiangjsoncraft
2501_944525541 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 预算详情页面
android·开发语言·前端·javascript·flutter·ecmascript
lxl13071 小时前
学习C++(5)运算符重载+赋值运算符重载
学习
打小就很皮...1 小时前
《在 React/Vue 项目中引入 Supademo 实现交互式新手指引》
前端·supademo·新手指引
C澒1 小时前
系统初始化成功率下降排查实践
前端·安全·运维开发