当前位置: 首页 > news >正文

前端学习日记(十七)

一.React

1.Redux

1.1实现计数器

<button id="decrement">-</button>
<span id="count">0</span>
<button id="increment">+</button><script src="https://unpkg.com/redux@latest/dist/redux.min.js"></script><script>// 定义reducer函数 // 内部主要的工作是根据不同的action 返回不同的statefunction counterReducer (state = { count: 0 }, action) {switch (action.type) {case 'INCREMENT':return { count: state.count + 1 }case 'DECREMENT':return { count: state.count - 1 }default:return state}}// 使用reducer函数生成store实例const store = Redux.createStore(counterReducer)// 订阅数据变化store.subscribe(() => {console.log(store.getState())document.getElementById('count').innerText = store.getState().count})// 增const inBtn = document.getElementById('increment')inBtn.addEventListener('click', () => {store.dispatch({type: 'INCREMENT'})})// 减const dBtn = document.getElementById('decrement')dBtn.addEventListener('click', () => {store.dispatch({type: 'DECREMENT'})})
</script>

1.2Redux数据流架构

  1. state: 一个对象 存放着我们管理的数据
  2. action: 一个对象 用来描述你想怎么改数据
  3. reducer: 一个函数 根据action的描述更新state

2.Redux与React-实现counter

2.1创建counterStore

import { createSlice } from '@reduxjs/toolkit'const counterStore = createSlice({// 模块名称独一无二name: 'counter',// 初始数据initialState: {count: 1},// 修改数据的同步方法reducers: {increment (state) {state.count++},decrement(state){state.count--}}
})
// 结构出actionCreater
const { increment,decrement } = counter.actions// 获取reducer函数
const counterReducer = counterStore.reducer// 导出
export { increment, decrement }
export default counterReducer
import { configureStore } from '@reduxjs/toolkit'import counterReducer from './modules/counterStore'export default configureStore({reducer: {// 注册子模块counter: counterReducer}
})

2.2注入store 

import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
// 导入store
import store from './store'
// 导入store提供组件Provider
import { Provider } from 'react-redux'ReactDOM.createRoot(document.getElementById('root')).render(// 提供store数据<Provider store={store}><App /></Provider>
)

3.Redux与React-提交action传参

4.Redux与React-异步action处理

import { createSlice } from '@reduxjs/toolkit'
import axios from 'axios'const channelStore = createSlice({name: 'channel',initialState: {channelList: []},reducers: {setChannelList (state, action) {state.channelList = action.payload}}
})// 创建异步
const { setChannelList } = channelStore.actions
const url = 'http://geek.itheima.net/v1_0/channels'
// 封装一个函数 在函数中return一个新函数 在新函数中封装异步
// 得到数据之后通过dispatch函数 触发修改
const fetchChannelList = () => {return async (dispatch) => {const res = await axios.get(url)dispatch(setChannelList(res.data.data.channels))}
}export { fetchChannelList }const channelReducer = channelStore.reducer
export default channelReducer
http://www.lryc.cn/news/607368.html

相关文章:

  • Ant 构建java项目
  • FastDDS (SharedMemory)
  • webpack面试题及详细答案80题(41-60)
  • C++ 前缀和、双指针
  • Node.js中Buffer的用法
  • 嵌入式第十七课!!!!位运算!!!
  • 考取锅炉司炉工证需要学习哪些专业知识?
  • Linux 用户与组管理:从配置文件到实操命令全解析
  • golang的函数
  • YOLO V11 + BotSort行人追踪定位项目
  • 风光储并离网切换仿真模型(下垂控制一次调频)
  • 详解K8s集群搭建:从环境准备到成功运行
  • 【问题思考总结】CART树如何剪枝?从CART树的生成到剪枝以及为什么CTt一定小于Ct?【图文】
  • 在多租户或多服务共享 Redis 时,如何做逻辑隔离或权限控制?
  • 【数据结构】-----排序的艺术画卷
  • ESD监控系统确保工厂生产设备的静电安全
  • 浏览器【详解】内置Observer(共五种,用于前端监控、图片懒加载、无限滚动、响应式布局、生成安全报告等)
  • cesium FBO(四)自定义相机渲染到Canvas(离屏渲染)
  • 开源工具FossFLOW,绘制技术图表
  • 嵌入式GPU图像渲染工具全景实用指南(i.MX8MP平台)
  • Python深度解析与爬虫进阶:从理论到企业级实践
  • ubuntu 镜像克隆
  • Git 实现原理剖析
  • 【编号394】阿姆河流域土地利用分布数据(1990-2015)
  • 智能问数系统的调研
  • 【工具分享】模拟接口请求响应的Chrome插件ModResponse
  • 什么是doris
  • 第七章 愿景12 小萍分享《人性的弱点》
  • 软件性能优化:善用80-20法则,精准突破瓶颈
  • grafana/lock-stack 日志 Pipeline 配置