钦州住房和城乡建设局网站,自己建的网站地址,上海企业信息,大连长建个人主页目录 一、react基础 5.loadsh使用排序8.ref获取DOM对象10.props使用*13.UseEffect 二、 react使用redux三、美团外卖项目完成页面制作使用redux渲染页面使用react-router-dom评价 一、react基础
jsx 大括号的作用
{count}
{userLlist.map((item){return li key{item… 目录 一、react基础 5.loadsh使用排序8.ref获取DOM对象10.props使用*13.UseEffect 二、 react使用redux三、美团外卖项目完成页面制作使用redux渲染页面使用react-router-dom评价 一、react基础
jsx 大括号的作用
{count}
{userLlist.map((item){return li key{item.id}{item.name}/li}) }
key是用于react内部渲染处理的基础条件渲染
{flag p{显示}/p}复杂条件渲染
function getItem(){if(type1) return p有图模式p/else if(type2) return 无图/}2、useState使用 state是状态变量数据变化视图变化
const [userList,setUserList]useState([{id:1,name:z}])3.map,filter函数使用 4.tab切换功能实现
const tab{{type: ,text: }}
//将type设为class高亮5.loadsh使用排序
import * as _ from lodash
setCommentList(_.orderby(commentList,star//字段名desc))
//不改变原数组6.classnames优化类名控制 7.受控绑定表单
input value{value} onChange{(e){setValue(e.target.value); //显示输入}}typetext/8.ref获取DOM对象
const inputRefuseRef(null);
input ref{inputRef}/
//使用inputRef.current获取DOM对象9.uuid和dayjs库的使用
10.props使用
用于父传子组件消息
function Son(prop){prop.nameprop.children //特殊属性
}
function Father(prop){return Son namexxxpthis is spanp//Son
}11.子传父兄弟之间传消息(利用子传父父传兄弟)
12.使用Context跨层通信
1、全局中createContext创建一个Context上下文对象
2、上层用Context.Provider value{mdg}/传递数据
3、底层用useContext(Context)方法获取数据*13.UseEffect const [commentList,setCommentList]useState([])useEffect((){async function getCommentList(){// async异步await等待const resawait fetch(/comment).then(resres.json().then(data{console.log(data);return data})) setCommentList(res);}getCommentList()},[])清除副作用最常见是在组件卸载时候 14.自定义hook
构造一个use开头的函数名在函数体内封装可复用的逻辑把组件中用到的状态变量return出去 使用时解构出来 15.hook使用规则 1.组件外使用 x 2.iffor循环内使用 x 16.json-server和axios使用
二、 react使用redux 在我们继续之前你需要熟悉一些重要的 Redux 术语
stateactionaction 是一个具有 type 字段的普通 JavaScript 对象reducer是一个函数接收当前的 state 和一个 action 对象必要时决定如何更新状态并返回新状态。函数签名是(state, action) newState。 你可以将 reducer 视为一个事件监听器它根据接收到的 action事件类型处理事件。dispatch,调用actionselector
三、美团外卖项目 添加按钮怎么显示 解决方法antd icon 滚动菜单如何实现 解决方法将延长的区域设置overflow:autoflex flex:1 不管内容多少一般都是平分空间空间大小都一致、 而 flex:auto 是根据内容的大小来分不是平的除非内容都是一样才平分
完成页面制作 使用redux渲染页面
完成购物车功能
store.js
// 编写storeimport { createSlice } from reduxjs/toolkit
import axios from axiosconst foodsStore createSlice({name: foods,initialState: {// 商品列表foodsList: [],// 菜单激活下标值activeIndex: 0,// 购物车列表cartList: []},reducers: {// 更改商品列表setFoodsList (state, action) {// payload是传入的值state.foodsList action.payload},changeActiveIndex(state,action){state.activeIndex action.payload},addCart (state, action) {// 是否添加过以action.payload.id去cartList中匹配 匹配到了 添加过const item state.cartList.find(item item.id action.payload.id)if (item) {item.count} else {state.cartList.push(action.payload)}},// count增increCount (state, action) {// 关键点找到当前要修改谁的count idconst item state.cartList.find(item item.id action.payload.id)item.count},// count减decreCount (state, action) {// 关键点找到当前要修改谁的count idconst item state.cartList.find(item item.id action.payload.id)if (item.count 0) {return}item.count--},clearCart(state){state.cartList[];},}
})// 异步获取部分
const { setFoodsList, changeActiveIndex, addCart, increCount, decreCount, clearCart } foodsStore.actions
const fetchFoodsList () {return async (dispatch) {// 编写异步逻辑const res await axios.get(meituan/menu)// 调用dispatch函数提交actiondispatch(setFoodsList(res.data))}
}export { fetchFoodsList, changeActiveIndex, addCart, increCount, decreCount, clearCart }const reducer foodsStore.reducerexport default reducer使用react-router-dom
制作路由表
const router createBrowserRouter([{path:login,element:DempPage/},{path:index,element:DempPage/}
])声明式导航
Link to/index /编程式导航
useNavigate()Routes 多个Route需要Route包起来RouteOutle 给children占位
评价 在img标签里面只设置宽度不设置高度图片就会等比例缩放。