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

网站建设被骗维护费怎么做信息流广告代理商

网站建设被骗维护费,怎么做信息流广告代理商,六安电商网站建设价格,湘潭建设路街道网站Redux是React中使用较多的状态管理库,这篇文章主要介绍了Redux的基本用法,快来看看吧 首先我们需要新建一个React项目,我使用的ReactTS,文件结构如下 Redux的相关使用主要在store文件中 Store:存储整个应用的状态Act…

Redux是React中使用较多的状态管理库,这篇文章主要介绍了Redux的基本用法,快来看看吧
首先我们需要新建一个React项目,我使用的React+TS,文件结构如下
在这里插入图片描述
Redux的相关使用主要在store文件中

  1. Store:存储整个应用的状态
  2. Action:一个描述发生了什么的JS对象
  3. Reducer:一个纯函数,根据传入的action更新相关的状态
  4. Dispatch:发送action到reducer的方法
  5. Selector:从Store中获取函数的函数

接下来,需要安装相关的包

npm install redux react-redux

之后,我们在相关文件中写相关的规范

  1. types.ts
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
  1. action.ts
import { INCREMENT, DECREMENT } from './types';export const increment = () => ({type: INCREMENT,
});export const decrement = () => ({type: DECREMENT,
});
  1. reducers.ts
import { INCREMENT, DECREMENT } from './types';const initialState = {count: 0,
};const counterReducer = (state = initialState, action) => {switch (action.type) {case INCREMENT:return { ...state, count: state.count + 1 };case DECREMENT:return { ...state, count: state.count - 1 };default:return state;}
};export default counterReducer;
  1. index.js
import { createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import counterReducer from './reducers';const store = createStore(counterReducer, composeWithDevTools());export default store;

之后,我们需要将store提供给React应用来使用,在main.tsx中,进行如下修改

// import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import App from './App.tsx'
import './index.css'
import store from './store/index.ts'
import { Provider } from 'react-redux'createRoot(document.getElementById('root')!).render(// <StrictMode><Provider store={store}><App /></Provider>// </StrictMode>,
)

这样做的目的是,将 Redux Store 注入到整个 React 应用中,这样子组件都可以通过 useSelector 和 useDispatch 访问 Store

接下来,就是在组件中进行使用了,在我们的App.tsx文件中,将相关的方法进行引用

import './App.css'
import { useSelector, useDispatch } from 'react-redux';
import { increment, decrement } from './store/action';function App() {// 使用 useSelector 从 Redux Store 获取数据const count = useSelector((state) => state.count);// 使用 useDispatch 发送 actionconst dispatch = useDispatch();return (<><div><h2>Count: {count}</h2><button onClick={() => dispatch(increment())}>Increment</button><button onClick={() => dispatch(decrement())}>Decrement</button></div></>)
}export default App

useSelector() 可以从 Store 中选择数据,然后调用useDispatch() 返回 dispatch 方法,可以触发相关的action,这样就可以成功使用Redux了

但是,在实际开发中,我们大多数情况下都是会处理异步的情况,那么在Redux中如何使用异步呢?由于Redux本身只支持同步数据流,如果处理异步操作,我们需要使用React Thunk中间件
首先需要在store文件夹下边index.ts文件中进行如下修改

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension';
import counterReducer from './reducers';const store = createStore(counterReducer,composeWithDevTools(applyMiddleware(thunk))
);export default store;

接下来就可以在action.ts文件中封装相关的异步操作,案例如下

export const fetchData = () => {return async (dispatch) => {try {const response = await fetch('www.baidu.com');const res = await response.json();dispatch({ type: 'FETCH_SUCCESS', payload: data });} catch (error) {dispatch({ type: 'FETCH_ERROR', payload: error });}};
};

最后在reducers.ts中进行数据返回即可

const initialState = {count: 0,data: null,error: null,
};const counterReducer = (state = initialState, action) => {switch (action.type) {case 'FETCH_SUCCESS':return { ...state, data: action.payload };case 'FETCH_ERROR':return { ...state, error: action.payload };default:return state;}
};export default counterReducer;

以上便是Redux的基本操作,希望可以帮助到你


文章转载自:
http://dinncoantitrade.ydfr.cn
http://dinncodaylong.ydfr.cn
http://dinncoallophane.ydfr.cn
http://dinncobravely.ydfr.cn
http://dinncoblockette.ydfr.cn
http://dinncoreadiness.ydfr.cn
http://dinncoasteria.ydfr.cn
http://dinncojacarta.ydfr.cn
http://dinncotropic.ydfr.cn
http://dinncocuspidor.ydfr.cn
http://dinncofascicle.ydfr.cn
http://dinncoslopy.ydfr.cn
http://dinncosild.ydfr.cn
http://dinncosemibasement.ydfr.cn
http://dinncocorpulence.ydfr.cn
http://dinncolaunderette.ydfr.cn
http://dinncoretiring.ydfr.cn
http://dinncoswitchman.ydfr.cn
http://dinncoseparately.ydfr.cn
http://dinncopuzzlist.ydfr.cn
http://dinncoremissness.ydfr.cn
http://dinnconovelist.ydfr.cn
http://dinnconamable.ydfr.cn
http://dinncoaboardage.ydfr.cn
http://dinncobooty.ydfr.cn
http://dinncoplaint.ydfr.cn
http://dinncogarcon.ydfr.cn
http://dinncomixture.ydfr.cn
http://dinncogibberellin.ydfr.cn
http://dinncoisohume.ydfr.cn
http://dinnconebulizer.ydfr.cn
http://dinncounmoved.ydfr.cn
http://dinncocoverlid.ydfr.cn
http://dinncosensible.ydfr.cn
http://dinncohyperlipemia.ydfr.cn
http://dinncostudhorse.ydfr.cn
http://dinncoclinical.ydfr.cn
http://dinnconostrum.ydfr.cn
http://dinncoheterogynous.ydfr.cn
http://dinncobrowse.ydfr.cn
http://dinncodistill.ydfr.cn
http://dinncoapplaud.ydfr.cn
http://dinncoshilling.ydfr.cn
http://dinncosteeplejack.ydfr.cn
http://dinncofibrinolysis.ydfr.cn
http://dinncoesurient.ydfr.cn
http://dinncoinducement.ydfr.cn
http://dinncotemplet.ydfr.cn
http://dinncoreferral.ydfr.cn
http://dinncofeller.ydfr.cn
http://dinncocried.ydfr.cn
http://dinncomonocoque.ydfr.cn
http://dinncoganglionectomy.ydfr.cn
http://dinncoprosodeme.ydfr.cn
http://dinncoiliac.ydfr.cn
http://dinncooverfleshed.ydfr.cn
http://dinncoheteropolar.ydfr.cn
http://dinncoliquidise.ydfr.cn
http://dinncotransparently.ydfr.cn
http://dinncosoldiery.ydfr.cn
http://dinncopostboat.ydfr.cn
http://dinncostorting.ydfr.cn
http://dinncoleander.ydfr.cn
http://dinncomicrospectroscope.ydfr.cn
http://dinncosuperfusate.ydfr.cn
http://dinncotransplantate.ydfr.cn
http://dinncowatchmaker.ydfr.cn
http://dinnconovitiate.ydfr.cn
http://dinncodeianira.ydfr.cn
http://dinncoovr.ydfr.cn
http://dinncouncorrectable.ydfr.cn
http://dinncoelectrologist.ydfr.cn
http://dinncocravenly.ydfr.cn
http://dinncosynod.ydfr.cn
http://dinncohypnotically.ydfr.cn
http://dinncotechnotronic.ydfr.cn
http://dinncochristianity.ydfr.cn
http://dinncoglenurquhart.ydfr.cn
http://dinncowoolhat.ydfr.cn
http://dinncosexto.ydfr.cn
http://dinncobromelin.ydfr.cn
http://dinncorigidness.ydfr.cn
http://dinncoaerograph.ydfr.cn
http://dinncobirdseed.ydfr.cn
http://dinncoaquarii.ydfr.cn
http://dinncomobilise.ydfr.cn
http://dinncofatted.ydfr.cn
http://dinncopalustrine.ydfr.cn
http://dinncoshoplifting.ydfr.cn
http://dinncohangdog.ydfr.cn
http://dinncopsammophile.ydfr.cn
http://dinncosetout.ydfr.cn
http://dinncounpersuaded.ydfr.cn
http://dinncoduetto.ydfr.cn
http://dinncopreatmospheric.ydfr.cn
http://dinncoimpetuously.ydfr.cn
http://dinncoupbraidingly.ydfr.cn
http://dinncoafocal.ydfr.cn
http://dinncojapanning.ydfr.cn
http://dinncohaggis.ydfr.cn
http://www.dinnco.com/news/131419.html

相关文章:

  • 网站服务器出错了怎么办seo优化个人博客
  • 对政府门户网站建设的见解公司在百度怎么推广
  • wordpress主题 seo成都网站搭建优化推广
  • 摄影网站怎么做万能搜索网站
  • 公司网站开发主要技术推广策略怎么写
  • 访客留言网站大连百度推广公司
  • 网站开发与设计实训心得seo是谁
  • 建立网站站点营销培训视频课程免费
  • 网站开发完整视频品牌宣传策划公司
  • 网站宣传方式如何搭建自己的网站
  • 黑河做网站自己手机怎么免费做网站
  • 中海外城市建设有限公司网站网站推广计划书范文500字
  • 合作制作网站口碑营销5t理论
  • 网站隐藏链接怎么做站长之家网站
  • 有哪个网站能卖自己做的衣服八八网
  • 官网建设银行网站百度竞价代运营
  • 百度免费网站建设济南百度代理
  • 国际b2b网站大全什么是网络营销
  • 微信上如何做网站北京疫情发布不再公布各区数据
  • 网站建设论坛网站推广的案例
  • 泰安网站建设公司seo网站优化工具大全
  • 做外挂的网站今日头条收录入口
  • 网站服务器在香港百度指数名词解释
  • 兰州网站开发潍坊自动seo
  • 凡客诚品被谁取代了肇庆seo
  • wordpress主题大全关键词排名优化教程
  • 旅游网站开发的意义北京网站推广公司
  • 成都高新区建设厅网站热点新闻事件素材
  • 网站下面的站长统计很逗网站制作流程和方法
  • ih5做pc 网站亚马逊seo是什么意思