基于 TypeScript 的 React 应用中使用 Redux 管理 NPM API 数据
介绍如何在使用 Create React App 创建的 TypeScript React 应用中,利用 Redux 管理从 NPM API 获取的数据。
项目初始化
使用 Create React App 创建一个新的 TypeScript React 项目:
npx create-react-app my-app --template typescript
安装依赖
安装 Redux 和相关依赖:
npm install redux react-redux @reduxjs/toolkit axios
创建 Redux Store
创建一个 Redux store 来存储应用状态,包括从 NPM API 获取的数据。
定义 Action
定义从 NPM API 获取数据的 action。
创建 Reducer
创建 reducer 来处理 action 并更新 store 中的数据。
连接 React 组件
使用 useSelector
和 useDispatch
hooks 将 React 组件连接到 Redux store。
获取数据
在组件加载时,使用 useEffect
hook 从 NPM API 获取数据。
展示数据
在 React 组件中展示从 Redux store 获取的数据。
fetch-npm-api-redux-typescript-master.zip
预估大小:24个文件
fetch-npm-api-redux-typescript-master
文件夹
.gitignore
310B
package.json
1KB
package-lock.json
643KB
src
文件夹
components
文件夹
RepositoriesList.tsx
865B
App.tsx
299B
state
文件夹
store.ts
201B
391.1KB
文件大小:
评论区