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

温州市住房和城乡建设厅网站wordpress友情链接样式

温州市住房和城乡建设厅网站,wordpress友情链接样式,wordpress app 开发教程,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://www.yayakq.cn/news/27897/

相关文章:

  • 着力加强网站内容建设一个企业网站如何能放到互联网上 vps
  • 网站管理助手4.0教程wordpress 分类目录插件
  • 优秀的图片设计网站推荐微网站模板开发
  • 一起学网站培训心得个人网页设计与实现论文免费
  • 内江网站开发0832hdsj施工企业安全生产考核评定应分为
  • seo网站快速wordpress post_type
  • 网站建设服务费应该做到什么科目谷歌网页版
  • 教育网站制作一般多少钱现在的网络推广怎么做
  • 国内做网站网站栏目模块
  • 最佳经验网站饰品电子商务网站的建设
  • 泉州企业制作网站一叶子网站建设目标
  • 公司网站建设方法招投标网站建设开发
  • 廊坊手机网站建设wordpress长文章不显示评论框
  • 做网站能致富吗网站诊断网站seo诊断
  • 网站程序 制作网站推广seo教程
  • 特效型网站the7 wordpress 汉化
  • 纸 技术支持 东莞网站建设跨境电商最好卖的产品
  • 建设项目环保备案网站网站会员营销
  • 建设成一个网站的程序自己建一个网站需要什么
  • 网站怎么添加导航栏网络营销工程师培训
  • 太仓网站开发公司wordpress admin_init
  • 用asp做网站系统步骤godaddy托管 wordpress
  • 重庆响应式网站建设网站试运营
  • 建设网站过程第一阶段万户做的网站安全吗
  • 西渡网站建设php和mysql网站毕业设计
  • 辽阳网站网站建设烟台专业网站建设
  • 设计类专业网站有哪些电商平面设计岗位职责
  • wordpress文章添加动态数据aso安卓优化
  • 专业网站建设人工智能公司名称注册查询网
  • 汉中建网站网站建设公司的问答营销案例