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

windows搭建php网站开锁做网站哪个好

windows搭建php网站,开锁做网站哪个好,python能做网站开发吗,wordpress电子书下载redux团队先后推出了redux、react-redux、reduxjs/toolkit#xff0c;这三个库的api各有不同。本篇文章就来梳理一下当我们需要在项目中集成redux#xff0c;从直接使用redux#xff0c;到使用react-redux#xff0c;再到react-redux和reduxjs/toolkit配合使用#xff0c;…redux团队先后推出了redux、react-redux、reduxjs/toolkit这三个库的api各有不同。本篇文章就来梳理一下当我们需要在项目中集成redux从直接使用redux到使用react-redux再到react-redux和reduxjs/toolkit配合使用分别需要调用到哪些api。 一、在react中集成redux redux中通过createStore可以创建出store实例在该实例上存在三个实例方法。 通过store.subscribe可以监听到state的改变引起组件的重新渲染。 通过dispatch可以分发action引起redux内部state的更新。 通过getState可以获取到redux内部的state。 以一个简单的计数器程序作为示例。 首先在App组件中引入Count组件为其传入store实例作为props。 import React from react; import Count from ./components/Count; import store from ./store;export default function App() {return Count store{store} /; }在Count组件中我们在组件挂载后通过store.subscribe监听redux的state通过store,getState获取state为按钮绑定点击事件回调调用dispatch传入action的返回值作为参数。 import React, { useEffect, useState } from react; import store from ../store; import { createAddActions } from ../store/actions/count;export default function Count() {const [_, rerender] useState({});useEffect(() {store.subscribe(() rerender({}));}, []);return (divdivsum: {store.getState()}/divbutton onClick{() store.dispatch(createAddActions(1))}点我1/button/div); }接下来是store的编写。 // store/index.jsimport { countReducer } from ./reducers/count; import { createStore } from redux;export default createStore(countReducer); reducers必须是一个纯函数纯函数的概念是需要返回一个新的state替换原来的state而不是直接在原来的state上修改。 // store/reducers/count.jsexport function countReducer(prevState, action) {switch (action.type) {case add:return prevState action.value;default:return 0;}; }; action的实现。 // store/actions/count.jsexport function createAddActions(value) {return {type: add,value,}; }; 二、在react中集成react-redux react-redux提出了容器组件的概念要求redux只能和容器组件进行通信至于ui组件只能通过props来获取容器组件传入的state和引起redux内部state改变的callback。 引入容器组件的目的在于尽量让ui组件内部看起来与其他不需要使用redux的组件无异。 如何理解无异呢就是不要直接去调用store的实例方法而是调用父组件提供的方法。当我们调用父组件提供的方法而react-redux会再调用store的实例方法。 store.getState() - props.state store.dispatch() - props.callback() store.subscribe不再手动调用而是由react-redux执行。 Count.js修改如下。 import React, { useEffect, useState } from react; import { connect } from react-redux; import { createAddActions } from ../store/actions/count;function Count(props) {return (divdivsum: {props.state}/divbutton onClick{() props.add(1)}点我1/button/div); }export default connect((state) ({ state }),(dispatch) ({add: (number) dispatch(createAddActions(number)),}) )(Count); connect的第二个参数还可以简写为对象形式。 export default connect((state) ({ state }), { add: createAddActions })(Count);我们使用redux的目的是在多个组件之间共享state所以我们直接引用redux时需要在多个组件中传入store作为propsreact-redux针对此也做了优化。 我们只需引入Provider包裹App组件在Provider中传入store作为props我们就可以在容器组件中获取到props。其实底层就是调用了react的SomeContext.Provider内部组件只要使用useContext就可以获取到store。 // index.jsimport React from react; import ReactDOM from react-dom/client; import { Provider } from react-redux; import store from ./store; import App from ./App;const root ReactDOM.createRoot(document.getElementById(root)); root.render(Provider store{store}App //Provider ); 在Count组件中我们不再需要手动传入store。 import React from react; import Count from ./components/Count;export default function App() {return Count /; } 在现在的react-redux中更推荐使用useSelector和useDispatch来代替connect。当我们使用useSelector和useDispatch获取state和分发action时不再存在container组件取而代之的是selector hook和dispatch hook。 对于connect而言会优先从props中获取store不存在的情况下再用React.useContext获取props所以store有两种传入方式。 对于useSelector和useDispatch而言他们只是Hook而不是组件没有props只能从React.useContext中获取store所以如果外层没有Provider组件的话项目是无法运行的。 此外connect和Provider都实现了对store的监听。 import React, { useEffect, useState } from react; import { useSelector, useDispatch } from react-redux; import { createAddActions } from ../store/actions/count;export default function Count() {const state useSelector((state) state);const dispatch useDispatch();return (divdivsum: {state}/divbutton onClick{() dispatch(createAddActions(1))}点我1/button/div); }三、在React中集成reduxjs/toolkit和react-redux reduxjs/toolkit的作用如下 1. 将initialState、reducers、actions整合在一起形成切片 2. 自动生成action{name: sliceName/reducerName, payload: value} 3. 代替redux/thunk实现异步action功能 // store/slices/count.jsimport { createSlice } from reduxjs/toolkit;const counterSlice createSlice({name: counter,initialState: 0,reducers: {add: (state, action) {return state action.payload;},}, });export const { add } counterSlice.actions;export default counterSlice.reducer; // store/index.jsimport countReducer from ./slices/count; import { configureStore } from reduxjs/toolkit;export default configureStore({reducer: countReducer // reducer的结构对应state的结构 });
http://www.tj-hxxt.cn/news/233132.html

相关文章:

  • 小说网站怎么做有域名之后怎么做网站
  • 网站建设摊销年限小刘网站建设
  • 商城网站源码dede深圳公司注册流程及资料
  • wordpress 仿站 菜单购物网站商城
  • 宣传型商务网站网站建设深圳哪家好
  • 百度收录左侧带图片的网站自适应营销网站模板
  • 算命网站建设wordpress 加入js
  • 外贸seo网站推广广东手机网站建设费用
  • 重庆网站建设哪个公司好门店管理系统软件排行
  • 在家做网站设计单位建网站的优势
  • 海洋网络网站建设陕西网站建设优化技术
  • 上海做网站建设的公司排名壶关网站建设
  • 个人备案能公司网站哪些网站可以免费看剧
  • 如何用网页设计制作个人网站借款网站模板
  • 网站建设网课emlog怎么转wordpress
  • wordpress留言墙重庆seo网站推广优化
  • 免费做自荐书的网站设计师网上接私单app
  • 长沙网站优化seo福州做网站互联网公司排名
  • 凤山县住房和城乡建设局网站tomcat做网站属于什么
  • 怎么做仲博注册网站专做和田玉的网站
  • 产品单页营销型网站模板下载电子商城网站制作公司
  • 物流网站建设方案wordpress drupal joomla
  • 网站建设用net后缀如何商品网站怎么做的
  • 整站优化网站网络运营与推广
  • 网站建设 业务培训长春怎么做网站
  • 苏州网站排名电子商务网站推广实训心得
  • 网站变黑白代码权4网站怎么做
  • 东莞建外贸企业网站怎么在网站做谷歌广告
  • 低多边形生成网站网站仿做
  • 建一个网站需要多少钱?wordpress链接的index.php