TypeScript-Webpack-React-Redux 项目教程

TypeScript-Webpack-React-Redux 项目教程

typescript-webpack-react-redux-boilerplateReact and Redux with TypeScript项目地址:https://gitcode.com/gh_mirrors/ty/typescript-webpack-react-redux-boilerplate

1. 项目的目录结构及介绍

typescript-webpack-react-redux-boilerplate/
├── src/
│   ├── actions/
│   ├── components/
│   ├── reducers/
│   ├── store/
│   ├── index.tsx
│   └── ...
├── .gitignore
├── .editorconfig
├── package.json
├── tsconfig.json
├── webpack.config.js
└── README.md
  • src/: 包含项目的源代码文件。
    • actions/: 存放 Redux 动作创建器。
    • components/: 存放 React 组件。
    • reducers/: 存放 Redux 减速器。
    • store/: 存放 Redux 存储配置。
    • index.tsx: 项目的入口文件。
  • .gitignore: 指定 Git 忽略的文件和目录。
  • .editorconfig: 配置编辑器样式和格式。
  • package.json: 项目的依赖和脚本配置。
  • tsconfig.json: TypeScript 编译配置。
  • webpack.config.js: Webpack 打包配置。
  • README.md: 项目说明文档。

2. 项目的启动文件介绍

项目的启动文件是 src/index.tsx,它负责初始化 React 应用并将其挂载到 DOM 中。以下是 index.tsx 的基本结构:

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import store from './store';
import App from './App';

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);
  • ReactDOM.render: 将 React 组件渲染到指定的 DOM 元素中。
  • Provider: 使 Redux 存储对整个应用可用。
  • store: Redux 存储实例。
  • App: 主应用组件。

3. 项目的配置文件介绍

package.json

package.json 文件包含了项目的依赖、脚本和其他元数据。以下是一些关键部分:

{
  "name": "typescript-webpack-react-redux-boilerplate",
  "version": "1.0.0",
  "scripts": {
    "start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --mode production"
  },
  "dependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-redux": "^7.2.4",
    "redux": "^4.1.0"
  },
  "devDependencies": {
    "@types/react": "^17.0.2",
    "@types/react-dom": "^17.0.2",
    "typescript": "^4.2.3",
    "webpack": "^5.28.0",
    "webpack-dev-server": "^3.11.2"
  }
}
  • scripts: 定义了启动和构建项目的命令。
  • dependencies: 生产环境依赖。
  • devDependencies: 开发环境依赖。

tsconfig.json

tsconfig.json 文件定义了 TypeScript 编译选项:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "es2015"],
    "module": "commonjs",
    "jsx": "react",
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true
  },
  "include": ["src"]
}
  • compilerOptions: 编译选项。
  • include: 指定包含的文件和目录。

webpack.config.js

webpack.config.js 文件定义了 Webpack 的打包配置:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.ts

typescript-webpack-react-redux-boilerplateReact and Redux with TypeScript项目地址:https://gitcode.com/gh_mirrors/ty/typescript-webpack-react-redux-boilerplate

  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

包力文Hardy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值