React 18

React + Redux + Router路由 + TS

安装插件 React Developer、Redux DevTools、极简插件;
babel
classnames库
dayjs

1 React

组件化开发方式、性能优秀(vnode、fiber)、丰富生态、跨平台支持(React native支持ios、安卓)

1.1 下载React项目

利用npx create-react-app react-basic下载项目,下载好项目之后启动项目

package.json中reactreact-dom是最核心的包;

index.js:项目入口文件,引入根组件

App.js:项目根组件

index.js文件

index.html文件

1.2 JSX

JSX需要通过解析工具babel,做解析之后才能在浏览器中运行;
https://babeljs.io/

1.3 Hooks

Hooks文章

const [count, setCount] = useState(0);
count状态变量 ,与普通js变量不同的是,状态变量值发生变化 会驱动 视图发生变化

count++ 会改变count 值,但不引发视图更新;想要触发视图更新,必须使用setCount

javascript 复制代码
let [count, setCount] = useState(0);
const handleClick = () => {
  count++;
  console.log(count); // count变为 1
  setCount(count + 1); // 视图更新后,count展示的结果是2
}

这里的重点是使用新值替换老值,不可以直接修改老值;对象也同理:setForm({...form, name: 'john'});

1.4 classnames库

classnames库

javascript 复制代码
import classNames from 'classnames';
className={classNames('nav-item', { active: type === 'hot' })}

2 Redux

Redux DevTools 扩展程序

Redux:React最常用的集中状态管理工具,可以独立于框架运行;

核心:stateactionreducer
Redux原理

  • Redux Toolkit:工具包;
  • react-redux:链接redux和react组件的中间件;
javascript 复制代码
npx create-react-app react-redux // 使用CRA快速创建React项目
npm i @reduxjs/toolkit react-redux // 安装配套工具
npm run start // 启动项目

在src目录下新建store目录

Redux store配置

React组件: 使用 react-redux注入store;使用/修改store中的数据

index.js文件

javascript 复制代码
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './modules/counterStore';

// 创建根store组合子模块
const store = configureStore({
  reducer: {
    counter: counterReducer
  }
});

export default store;

React组件index.js文件

javascript 复制代码
import store from './store';
import { Provider } from 'react-redux';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <Provider store={store}>
    <App />
  </Provider>
)
javascript 复制代码
import { useSelector, useDispatch } from 'react-redux';
import { decrement, increment } from '.store//modules/counterStore';

function App() {
  const { count } = useSelector(state => state.counter);
  // 选择counter对应的模块
  const dispatch = useDispatch();
  return (
  	<button onCLick={() => dispatch(decrement(10))}>-10</button>
    <div>{count}</div>
    <button onCLick={() => dispatch(increment(20))}>+20</button>
  )
}

export default App;
相关推荐
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
ew452182 小时前
ElementUI表格表头自定义添加checkbox,点击选中样式不生效
前端·javascript·elementui
suibian52352 小时前
AI时代:前端开发的职业发展路径拓宽
前端·人工智能
Moon.92 小时前
el-table的hasChildren不生效?子级没数据还显示箭头号?树形数据无法展开和收缩
前端·vue.js·html
垚垚 Securify 前沿站2 小时前
深入了解 AppScan 工具的使用:筑牢 Web 应用安全防线
运维·前端·网络·安全·web安全·系统安全
工业甲酰苯胺5 小时前
Vue3 基础概念与环境搭建
前端·javascript·vue.js
mosquito_lover16 小时前
怎么把pyqt界面做的像web一样漂亮
前端·python·pyqt
柴柴的小记9 小时前
前端vue引入特殊字体不生效
前端·javascript·vue.js
柠檬豆腐脑9 小时前
从前端到全栈:新闻管理系统及多个应用端展示
前端·全栈
bin915310 小时前
DeepSeek 助力 Vue 开发:打造丝滑的颜色选择器(Color Picker)
前端·javascript·vue.js·ecmascript·deepseek