开源项目 a-wp-react-redux-theme 使用教程
1. 项目的目录结构及介绍
a-wp-react-redux-theme/
├── assets/
│ ├── css/
│ ├── images/
│ └── js/
├── components/
│ ├── App.js
│ ├── Header.js
│ └── Footer.js
├── containers/
│ ├── Home.js
│ └── About.js
├── redux/
│ ├── actions/
│ ├── reducers/
│ └── store.js
├── templates/
│ ├── home.php
│ └── about.php
├── webpack.config.js
├── package.json
├── README.md
└── index.php
- assets/: 存放静态资源文件,如CSS、图片和JavaScript文件。
- components/: 存放React组件文件,如
App.js
、Header.js
和Footer.js
。 - containers/: 存放React容器组件文件,如
Home.js
和About.js
。 - redux/: 存放Redux相关的文件,包括actions、reducers和store配置。
- templates/: 存放WordPress模板文件,如
home.php
和about.php
。 - webpack.config.js: Webpack配置文件。
- package.json: 项目依赖和脚本配置文件。
- README.md: 项目说明文档。
- index.php: 项目入口文件。
2. 项目的启动文件介绍
- index.php: 这是项目的入口文件,负责加载React应用并渲染到页面上。它通常包含WordPress的主题初始化代码和React应用的挂载点。
<?php
/*
* 主题入口文件
*/
// 加载React应用
require_once(get_template_directory() . '/assets/js/app.js');
// 渲染React应用
function render_react_app() {
echo '<div id="root"></div>';
}
add_action('wp_footer', 'render_react_app');
3. 项目的配置文件介绍
- webpack.config.js: 这是Webpack的配置文件,定义了如何打包JavaScript、CSS等资源文件。
const path = require('path');
module.exports = {
entry: './assets/js/app.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
}
};
- package.json: 这是Node.js项目的配置文件,包含了项目的依赖和脚本命令。
{
"name": "a-wp-react-redux-theme",
"version": "1.0.0",
"description": "A WordPress theme with React and Redux",
"main": "index.js",
"scripts": {
"start": "webpack --watch",
"build": "webpack"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"redux": "^4.1.0",
"react-redux": "^7.2.4"
},
"devDependencies": {
"webpack": "^5.38.1",
"webpack-cli": "^4.7.2",
"babel-loader": "^8.2.2"
}
}
通过以上配置,你可以启动项目并进行开发或构建。