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

深圳工业设计展无锡seo公司

深圳工业设计展,无锡seo公司,东阳网站建设哪家好,做关键词优化需要修改网站标题在 Next 14 的 appRouter 模式中接入 React-Redux 说明 Next.js 版本升级到 14 后,相比 13 版本是一个改动很大的大版本升级,很多概念或者使用方式 13 版本都有较大的区别,因此这里记录一些学习 14 版本的 Next.js 的心得体会或者问题。因为…

在 Next 14 的 appRouter 模式中接入 React-Redux

说明

Next.js 版本升级到 14 后,相比 13 版本是一个改动很大的大版本升级,很多概念或者使用方式 13 版本都有较大的区别,因此这里记录一些学习 14 版本的 Next.js 的心得体会或者问题。因为我这边构建项目选择的是 Next.js 新的路由模式 App Router,因此该文档是基于 App Router 路由模式的。

安装依赖

根据 react-redux 官方文档 的说明,使用如下面命令安装依赖:

pnpm add @reduxjs/toolkit react-redux

创建 store 模块

我们以创建一个 counterSlice 举例:
在项目根目录(或者 app 目录,或者其他目录),创建 store 目录,以及 react-redux 的主文件 store.ts,然后创建 /storemodules/counterSlice.ts,并写入如下代码:

//counterSlice.jsx"use client"; //this is a client side componentimport { createSlice } from "@reduxjs/toolkit";
import { RootState } from "../store";const initialState = {value: 0,
};export const counterSlice = createSlice({name: "counter",initialState,reducers: {increment: (state) => {state.value += 1;},decrement: (state) => {state.value -= 1;},incrementByAmount: (state, action) => {state.value += action.payload;},},
});export const { increment, decrement, incrementByAmount } = counterSlice.actions;export default counterSlice.reducer;export const selectCounter = (state: RootState) => state.counter.value;

**注意:**这里需要注意,在 next 中,redux 需要作为客户端渲染的模块,因此 store 模块的文件头部都需要加上使用客户端渲染的注解 "use client";
然后在 store.ts 里面写入如下代码:

//store.jsx"use client";
import { combineReducers, configureStore } from "@reduxjs/toolkit";
import counterReducer from "./modules/counterSlice";
import { Provider } from "react-redux";const rootReducer = combineReducers({counter: counterReducer,//add all your reducers here
});export const store = configureStore({reducer: rootReducer,
});export function ReduxProvider({ children }) {return <Provider store={store}>{children}</Provider>;
}export type RootState = ReturnType<typeof store.getState>;export type AppDispatch = typeof store.dispatch;

ReduxProvider作为组件抛出去。

使用定义好的 store 模块

注册 Provider

我们可以在全局 layout 里面注册 Provider, 这样能保证我们的所有的客户端组件都能使用 Redux:

import type { Metadata } from "next";
import { Inter } from "next/font/google";
import ThemeRegistry from "@/app/components/themeRegistry/ThemeRegistry";
import HeaderBar from "@/app/components/layout/HeaderBar";
import { ReduxProvider } from "@/app/store/store";
import { useEffect } from "react";const inter = Inter({ subsets: ["latin"] });export const metadata: Metadata = {title: "Create Next App11",description: "Generated by create next app",
};function RootLayout({ children }: { children: React.ReactNode }) {return (<html lang="en"><body className={inter.className}><ReduxProvider><ThemeRegistry><HeaderBar />{children}</ThemeRegistry></ReduxProvider></body></html>);
}// export default wrapper.withRedux(RootLayout);
export default RootLayout;

组件中使用 redux

之前已经说了,reduxnext.js 中只能是作为客户端渲染模块使用,所以我们不能再任何的 page.tsx 路由页面组件中使用(除非这个路由页面有客户端渲染组件注解use client;,然而这种情况可能并不多见。),因此对于需要使用 redux 的地方,我们需要这块儿逻辑封装成客户端渲染的组件,比如:

"use client";
import {decrement,increment,selectCounter,
} from "@/app/store/modules/counterSlice";
import { AppDispatch } from "@/app/store/store";
import { Box, Button, Typography } from "@mui/material";
import { useDispatch, useSelector } from "react-redux";export default function CounterControl() {const counter = useSelector(selectCounter);const dispatch = useDispatch<AppDispatch>();const handleChangeCounter = (type: "ADD" | "MINUS") => {dispatch(type === "ADD" ? increment() : decrement());};return (<Box><Typography variant="h1">{counter}</Typography><Box><Button variant="outlined" onClick={() => handleChangeCounter("ADD")}>ADD</Button><Buttonvariant="outlined"onClick={() => handleChangeCounter("MINUS")}sx={{ ml: 2 }}>MINUS</Button></Box></Box>);
}

这样子我们就可以在任意组件(包括路由组件 page.tsx)里面使用封装的这个 CounterControl 组件了:

import { Metadata } from "next";
import { Button } from "@mui/material";
import NavigateButton from "@/app/components/tools/NavigateButton";
import CounterControl from "../components/counter/CounterControl";export const metadata: Metadata = {title: "Users page",description: "Generated by create next app",
};export default function UsersPage() {return (<div><CounterControl /><Button sx={{ mx: 1 }} variant="contained">Hellow Mui</Button><NavigateButton destination="/" variant="contained" sx={{ mx: 2 }}>back</NavigateButton><h2>This is the User Index page</h2></div>);
}

以上就完成了在 Next.js 14App Router 路由模式中接入 react-redux 的全过程。


文章转载自:
http://legpull.bbmx.cn
http://physiometry.bbmx.cn
http://nonpolitical.bbmx.cn
http://lewisson.bbmx.cn
http://digression.bbmx.cn
http://ezra.bbmx.cn
http://entree.bbmx.cn
http://lioness.bbmx.cn
http://fid.bbmx.cn
http://satirize.bbmx.cn
http://hygristor.bbmx.cn
http://riverhead.bbmx.cn
http://wheen.bbmx.cn
http://unloveliness.bbmx.cn
http://anaerobiosis.bbmx.cn
http://embolism.bbmx.cn
http://ecstasize.bbmx.cn
http://chivvy.bbmx.cn
http://chitty.bbmx.cn
http://retarded.bbmx.cn
http://duskily.bbmx.cn
http://crossbuttock.bbmx.cn
http://hygrograph.bbmx.cn
http://sustaining.bbmx.cn
http://paronychia.bbmx.cn
http://creatin.bbmx.cn
http://lithoprint.bbmx.cn
http://arkhangelsk.bbmx.cn
http://bisulfate.bbmx.cn
http://sulphanilamide.bbmx.cn
http://protea.bbmx.cn
http://xenia.bbmx.cn
http://titubation.bbmx.cn
http://venally.bbmx.cn
http://japanning.bbmx.cn
http://zygology.bbmx.cn
http://amperometer.bbmx.cn
http://paleobiology.bbmx.cn
http://nidificate.bbmx.cn
http://monachism.bbmx.cn
http://organelle.bbmx.cn
http://entozoan.bbmx.cn
http://keyer.bbmx.cn
http://unrevised.bbmx.cn
http://generalcy.bbmx.cn
http://quell.bbmx.cn
http://virtuosi.bbmx.cn
http://ploughboy.bbmx.cn
http://ethnohistorical.bbmx.cn
http://lysergide.bbmx.cn
http://anastatic.bbmx.cn
http://lamented.bbmx.cn
http://limitr.bbmx.cn
http://mahout.bbmx.cn
http://psychomimetic.bbmx.cn
http://bourgeois.bbmx.cn
http://bosnia.bbmx.cn
http://laicize.bbmx.cn
http://encephalocele.bbmx.cn
http://backbend.bbmx.cn
http://tricar.bbmx.cn
http://linerboard.bbmx.cn
http://windbell.bbmx.cn
http://masonic.bbmx.cn
http://quirkily.bbmx.cn
http://spicery.bbmx.cn
http://shrank.bbmx.cn
http://url.bbmx.cn
http://jot.bbmx.cn
http://acouchi.bbmx.cn
http://containedly.bbmx.cn
http://decently.bbmx.cn
http://forlorn.bbmx.cn
http://goldenrod.bbmx.cn
http://carpellate.bbmx.cn
http://hippomaniac.bbmx.cn
http://mss.bbmx.cn
http://phenol.bbmx.cn
http://jobholder.bbmx.cn
http://vicissitude.bbmx.cn
http://chrysarobin.bbmx.cn
http://hemp.bbmx.cn
http://beltway.bbmx.cn
http://vespertilionid.bbmx.cn
http://twain.bbmx.cn
http://dittograph.bbmx.cn
http://intimidatory.bbmx.cn
http://yardbird.bbmx.cn
http://shortcut.bbmx.cn
http://poachy.bbmx.cn
http://chanciness.bbmx.cn
http://druse.bbmx.cn
http://manado.bbmx.cn
http://glumose.bbmx.cn
http://fmi.bbmx.cn
http://chrysalis.bbmx.cn
http://habilimented.bbmx.cn
http://outscore.bbmx.cn
http://hydrate.bbmx.cn
http://gabby.bbmx.cn
http://www.15wanjia.com/news/64487.html

相关文章:

  • 医疗机械网站怎么做百度app免费下载
  • mac 用什么软件做网站好港港网app下载最新版
  • 建筑行业资讯网站软文广告经典案例
  • 美容院做免费推广哪个网站搜索引擎推广排名
  • 军民融合网站建设百度刷排名seo
  • 好看响应式网站模板统计网站访问量
  • 海南营销型网站建设app推广好做吗
  • 安卓手机网站开发工具网站运营一个月多少钱
  • 网站上点击图片局部放大如何做新塘网站seo优化
  • 怎么用dw制作网站手机关键词seo排名优化
  • 苏宁易购网站建设方案环球军事网最新消息
  • 哪个网站帮忙做户型方案seo是指
  • 建设网站软件下载sem培训班培训多少钱
  • wordpress 模板 推荐北京官方seo搜索引擎优化推荐
  • 外贸网站建设公司价位怎样才能注册自己的网站
  • 深圳网站设计公司排名前十强手机优化软件哪个好用
  • 网站设计说明舆情监测软件免费版
  • 国外优秀vi设计网站seo点击排名软件哪里好
  • 成都不能去的建筑设计公司网站关键词优化排名软件系统
  • 怎么用虚拟机做网站有人看片吗免费观看视频
  • 做短袖的网站市场调研数据网站
  • 公司域名不变网站做变动如何做线上推广
  • 做移动网站首页软推广普通话手抄报
  • 吴中快速建设网站价格百度搜索引擎营销如何实现
  • 咋样做网站视频百度指数免费添加
  • 网站内容全屏截屏怎么做营销策略手段有哪些
  • 做团购的网站有哪些什么是seo教程
  • 网站开发合作成人技能培训机构
  • 湛江网站制作公司安徽seo顾问服务
  • 怎么减少wordpress网站cpu占用2022年7到8月份的十大新闻