react-demo
React 示例项目 · 简易留言板。本项目拥有完善的文档说明与注释,让您快速上手 React 开发 SPA。Webpack / ES6 + Babel / Redux / React Router —— An Excellent React Starter,可能是东半球最佳的 React Starter ,基于 Vue Cli 二次开发
Top Related Projects
Set up a modern web app by running one command.
A JS library for predictable global state management
Declarative routing for React
🔥 A highly scalable, offline-first foundation with the best developer experience and a focus on performance and best practices.
Quick Overview
The kenberkeley/react-demo repository is a comprehensive React.js project template and tutorial. It provides a well-structured boilerplate for building React applications, along with detailed documentation and examples to help developers understand and implement best practices in React development.
Pros
- Extensive documentation and explanations for React concepts and project structure
- Includes a complete demo application showcasing various React features and patterns
- Provides a solid foundation for both beginners and experienced developers to start React projects
- Regularly updated to incorporate the latest React best practices and ecosystem changes
Cons
- May be overwhelming for absolute beginners due to the amount of information provided
- Some of the included libraries and tools might become outdated over time
- The project structure may not fit all use cases or personal preferences
- Documentation is primarily in Chinese, which may limit accessibility for non-Chinese speakers
Getting Started
To get started with the kenberkeley/react-demo project:
-
Clone the repository:
git clone https://github.com/kenberkeley/react-demo.git
-
Install dependencies:
cd react-demo npm install
-
Start the development server:
npm start
-
Open your browser and navigate to
http://localhost:8080
to view the demo application.
For more detailed instructions and explanations, refer to the project's README and documentation files.
Competitor Comparisons
Set up a modern web app by running one command.
Pros of create-react-app
- Official React toolchain with robust community support and regular updates
- Comprehensive out-of-the-box setup, including testing, building, and development tools
- Abstracts away complex configuration, allowing developers to focus on coding
Cons of create-react-app
- Less flexibility for custom configurations without ejecting
- Larger bundle size due to included features that may not be necessary for all projects
- Steeper learning curve for understanding the underlying build process
Code Comparison
react-demo:
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
ReactDOM.render(<App />, document.getElementById('root'))
create-react-app:
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
The code comparison shows that create-react-app includes additional features like StrictMode and performance monitoring (reportWebVitals), while react-demo provides a simpler setup. create-react-app also uses the new ReactDOM.createRoot API for concurrent rendering support.
A JS library for predictable global state management
Pros of Redux
- More comprehensive state management solution
- Larger community and ecosystem of middleware/tools
- Better suited for complex applications with intricate state logic
Cons of Redux
- Steeper learning curve and more boilerplate code
- Can be overkill for simple applications
- Requires additional setup and configuration
Code Comparison
Redux (store creation):
import { createStore } from 'redux'
import rootReducer from './reducers'
const store = createStore(rootReducer)
React Demo (component state):
import React, { useState } from 'react'
function Counter() {
const [count, setCount] = useState(0)
// ...
}
Redux focuses on centralized state management, while React Demo likely uses local component state. Redux requires reducers and actions, whereas React Demo can manage state with hooks or class components.
Redux is more suitable for large-scale applications with complex state interactions, while React Demo may be better for smaller projects or learning React basics. The choice between them depends on the project's complexity and specific requirements.
Declarative routing for React
Pros of React Router
- More comprehensive routing solution with advanced features like nested routes and dynamic routing
- Actively maintained with regular updates and a large community
- Extensive documentation and widespread adoption in the React ecosystem
Cons of React Router
- Steeper learning curve for beginners compared to simpler routing implementations
- Potentially overkill for small projects with basic routing needs
- Requires additional setup and configuration
Code Comparison
React Router:
import { BrowserRouter, Route, Switch } from 'react-router-dom';
<BrowserRouter>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
</Switch>
</BrowserRouter>
React Demo:
import { Router, Route } from 'react-router';
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="about" component={About} />
</Route>
</Router>
React Router offers a more modern and flexible approach to routing, with components like BrowserRouter
and Switch
for better control. React Demo uses an older version of React Router, showcasing a simpler but less feature-rich implementation. React Router's extensive features and active development make it a more robust choice for complex applications, while React Demo might be suitable for simpler projects or learning purposes.
🔥 A highly scalable, offline-first foundation with the best developer experience and a focus on performance and best practices.
Pros of react-boilerplate
- More comprehensive and feature-rich, offering a complete setup for large-scale applications
- Includes advanced features like code splitting, offline-first functionality, and i18n support
- Actively maintained with frequent updates and a large community
Cons of react-boilerplate
- Steeper learning curve due to its complexity and extensive feature set
- May be overkill for smaller projects or beginners learning React
- Requires more setup and configuration to get started
Code Comparison
react-boilerplate:
import { createSelector } from 'reselect';
import { initialState } from './reducer';
const selectHome = state => state.home || initialState;
const makeSelectUsername = () =>
createSelector(
selectHome,
homeState => homeState.username
);
react-demo:
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import store from 'STORE'
import App from 'COMPONENT/App'
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('app')
)
The code comparison shows that react-boilerplate uses more advanced concepts like selectors and reselect, while react-demo focuses on a simpler setup with basic Redux integration. This reflects the overall difference in complexity and feature set between the two projects.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
React 示ä¾é¡¹ç® · ç®æçè¨æ¿ + å¾ åäºé¡¹
2017.7 å级çé¢è§ï¼
react-scaffold
ï¼æ¬è¯·æå¾ ï¼
###åå¨åé¢
ä¸ç´ä»¥æ¥ï¼æé½ç¸å½çº³é·ï¼ä¸ºä»ä¹ React çé£äº starter kit é½æ建å¾é£ä¹æ¶å¿ï¼
è½ä¸è½å Vue Cli çæç项ç®æ¶æè¬ä¼é ï¼è¯´å¹²å°±å¹²ï¼æ¬é¡¹ç®å°±æ¹èª Vue Demoæ´æ°
2016/8/28 å¼å ¥
cross-env
解å³è·¨å¹³å°é®é¢ï¼æ°å¢ä¼å项DedupePlugin
2016/8/29 éå½åmakeContainer / makeReducer.js => createContainer / createReducer.js
2016/9/10 éæsrc/redux/
2016/10/15 åæ¥ Vue Demo çæ¹å¨
2016/10/16 æ¹è¿ï¼ä» å¨å¼å模å¼ä¸å¼å ¥ React Hot Loader
ç®å½
§ ææ¯æ
§ å¿«éå¼å§
§ 项ç®æ¶æ
§ å¼å
§ æµè¯
§ é¨ç½²
§ åè
§ ææ¯æ
详æ å¯åé
package.json
- React 15.3.0
- Redux
- React Router
- Ajax 请æ±åºï¼Superagent / jQuery-Ajax / ...ï¼
- Webpack
- ES6 + Babel
- jQuery + BootStrap (UI)
§ å¿«éå¼å§
å¨å¼å§åï¼å¸ææ¨å·²é读å¦ä¸èµæ
- React ææ¡£
- Redux ææ¡£ï¼çå®åæµé¼ç请转ç Redux èå¼æç¨ï¼
- React Router ææ¡£
åæ¶æ¨è¿éè¦çæ ES6ãä¾å¦ï¼è¯·æå¦ä¸ä»£ç
const foo = ({ hello: { world: bar } }) => ({ bar })
转è¯æ ES5ï¼çæ¡è¯·èªè¡å° Babel REPL å¨çº¿ç¼è¯éªè¯ï¼
â å®è£
æ¨èåçº§å° node 5.x/6.x + npm 3.x ç¯å¢ï¼å¼ºçæ¨è使ç¨
cnpm
å®è£ ä¾èµææå¨
åæ¢å°æ·å® npm æºï¼npm set registry https://registry.npm.taobao.org/
ï¼ç»æµè¯ï¼cnpm
对äºnode-sass
çé®é¢å¤å¤ç Package æ¥æç§æè½åï¼
æ¬ç¤ºä¾é¡¹ç®éè¦ç»å ç®æçè¨æ¿ RESTful API
模æåå端å离å¼åï¼è¿ä¸ºäºä¸ Vue Demo å
±ç¨ï¼
请åå« git clone
ï¼æå¼ä¸¤ä¸ªå½ä»¤çªå£ï¼ Windows ä¸æ¨èä½¿ç¨ Cygwin / Git Bash
ï¼**åå«**åæ¢å°ä¸¤è
çç®å½ä¸
åå«æ²ä¸ npm install
å®è£
ä¾èµï¼ä¸ºé¿å
Windows ä¸ npm 2.x ç软é¾æ¥é®é¢ï¼å¯å ä¸ --no-bin-link
å®å
¨è§£æææä¾èµï¼
â å¯å¨
å
åå¨ msg-board-api
ãreact-demo
çå½ä»¤çªå£ä¸ï¼æ²ä¸ npm start
å¦æ æå¤ï¼é»è®¤æµè§å¨å°±ä¼èªå¨æå¼ localhost:9090
ï¼æ¨ç«å³å¯ä»¥çå°ææ
è¥æµè§å¨æ²¡æèªå¨å¼¹åºï¼å请èªè¡æå¨è®¿é®
P.S. å¦ææ¨è¿ä¸æ¸ æ¥å¦ä½å®è£ ä¸å¯å¨ï¼è¯·çè¿ä¸ª issue
§ 项ç®æ¶æ
â ç®å½ç»æ
.
ââ build/ # Webpack é
ç½®ç®å½
ââ dist/ # build çæçç产ç¯å¢ä¸ç项ç®
ââ src/ # æºç ç®å½ï¼å¼åé½å¨è¿éè¿è¡ï¼
â ââ assets/ # æ¾ç½®éè¦ç»ç± Webpack å¤ççéææ件
â ââ components/ # ç»ä»¶ï¼COMPONENTï¼
â ââ redux/ # Redux ä¸ç®©ç
â â ââ actions/ # ï¼ACTIONï¼
â â ââ reducers/ # ï¼REDUCERï¼
â â ââ store/ # ï¼STOREï¼
â âââ routes/ # è·¯ç±ï¼ROUTEï¼
â âââ services/ # æå¡ï¼SERVICEï¼ç¨äºç»ä¸ç®¡ç XHR 请æ±ï¼è¿æ¯ä» Vue Demo ä¸ç´æ¥å¤å¶è¿æ¥çï¼
â âââ utils/ # å·¥å
·åºï¼UTILï¼
â â ââ HoC/ # é«é¶ç»ä»¶ï¼HOCï¼å
¨ç§° Higher Order Componentï¼
â â ââ mixins/ # æ··åï¼MIXINï¼
â âââ views/ # è·¯ç±è§å¾åºé¡µï¼VIEWï¼
â â ââ layout/ # å
¨å±å¸å±
â âââ app.js # å¯å¨æ件
â âââ index.html # éæåºé¡µ
âââ static/ # æ¾ç½®æ éç»ç± Webpack å¤ççéææ件
âââ .babelrc # Babel 转ç é
ç½®
âââ .eslintignore # ï¼é
ç½®ï¼ESLint æ£æ¥ä¸é忽ç¥çæ件ï¼å¤¹ï¼
âââ .eslintrc # ESLint é
ç½®
âââ .gitignore # ï¼é
ç½®ï¼é被 Git 忽ç¥çæ件ï¼å¤¹ï¼
âââ package.json # ï¼è¿ä¸ªå°±ä¸ç¨å¤è§£éäºå§ï¼
å¨è¿éæ¨å¯è½ä¼é®ï¼æä¹æ²¡æ containers/
ç®å½ï¼
å¨æ¬é¡¹ç®ä¸ï¼æ¨å¶ç»ä»¶ä¸æºè½ç»ä»¶æ大çå·®å«å¨äºï¼
åè
çç¶ææ¯éè¿ç¶ç»ä»¶ä¼ å
¥è·å¾ï¼èåè
æ¯ç´æ¥**è¿æ¥**å° state
è·å¾
亦å³ï¼è¥ä¸ä¸ªæ¨å¶ç»ä»¶ç´æ¥**è¿æ¥**å° state
ï¼é£ä¹å®å°±æ¯ä¸ä¸ªæè°çæºè½ç»ä»¶
ï¼è¯¦è§ src/utils/createContainer.js
ä¸å¯¹ react-redux
ç connect
å½æ°çå°è£
ï¼
æ¬ç¤ºä¾é¡¹ç®å¯ä¸å¨ç»ä»¶çå®ä¹ä¸èªè¡ä½¿ç¨ connect
å½æ°çæ¯ Navbar
ç»ä»¶ï¼ä¸ç¨å°äº ES7 çè£
饰å¨ï¼
æå ³æ¨å¶ç»ä»¶ä¸æºè½ç»ä»¶æ´ä¸ºç²¾ç¡®ç论述ï¼æ¨è Redux ä½è Dan çè¿ç¯æç« ï¼é¿å ææ¡ä¸»ä¹
æ¨å¯ä»¥æ ¹æ®ä¸å¡éæ±æ¹å¨ç®å½ç»æãè¥ç®å½ä½¿ç¨é¢ç¹ï¼å»ºè®®é ç½® è·¯å¾å«å
é»è®¤çè·¯å¾å«åè§ä¸é¢ç®å½ç»æ注éä¸å¤§åå½¢å¼ç常é
ç¹å«æ¨èï¼ æ´æ°çæ¬ç Vue Demo ææ¡£ · ç®å½ç»æ
â ç¹è²
- æ¬ç¤ºä¾é¡¹ç®ç§æ¿æä½³å®è·µï¼é«åº¦æ´çå°å®ç°ä»£ç å离/å¤ç¨
- ä¼åç®å½ç»æï¼æ´å¥½ç模åå离ï¼æ´æ¥è¿ Vue çå¼å模å¼
- Redux DevToolsï¼å¯é Chrome æ件形å¼ï¼é»è®¤ï¼ æ å åµé¡µé¢çç»ä»¶å½¢å¼
- Redux Logger æå°å¨ä½åååç¶æåå
- why-did-you-update æ£æµä¸å¿ è¦çç»ä»¶é渲æï¼é»è®¤å ³éï¼
- å¼å ¥æå¡å±ç»ä¸ç®¡ç XHR 请æ±ï¼å¥½å¤è¯·åè Vue Demo ä¸ç å¼å ¥æå¡å±ï¼
- å¼å ¥ è·¯å¾å«å å®ç°ä¼é çå 载模å¼
- å¼å ¥ React Hot Reloadï¼æ¯æçæ¿æ¢
- ç产ç¯å¢ä¸çç¼è¯å¯¹ä»£ç è¿è¡ä¼å
- è¿ä»ä¸ºæ¢æè§è¿çæå®ç¾ç starter kit
æå ³ Redux DevTools ä¸ why-did-you-update çå¯ç¨ä¸ç¦ç¨ï¼è§ä¸é¢ç å¼åç¯å¢å ¨å±åé é ç½®
§ å¼å
â Webpack é ç½®
ç±äºå·²ç»æ¥æç¸å¯¹æçç Webpack é ç½®ï¼å æ¤å¨ä¸å®ç¨åº¦ä¸æ¨å¯ä»¥ä¸æ±ç解ï¼ä½äºè§£å ¶é ç½®ä¼æ´è½ææ¡æ´ä½å¼å
- å端å¼åæå¡å¨ä¸º
localhost:9090
ï¼å¯å¨build/webpack.dev.conf.js
ä¸æ¾å°
å端 RESTful API åºå°ååå¨äº
src/services/xhr/config.js
ä¸ï¼è¯·æ ¹æ®å®é èªè¡ä¿®æ¹
- æ¡æ¶ / ç±»åº é¡»å离æå
以å å¿«å¼åæ¶çç¼è¯é度并æå©äºç¼åï¼è¯¦è§
build/webpack.base.conf.js
ä¸çvendor
å®é ä¸è¯¥æ¥éª¤å¯éè¿è¯»å
package.json
çdependencies
å段å®ç°èªå¨åï¼ä½å ¶çµæ´»åº¦ä¸å¤é«ï¼å¿ è¦æ§ä¹ä¸å¤§
P.S. å®è£ å æ¶å¿å¿--save / --save-dev
以添å ä¾èµè®°å½
- è·¯å¾å«å çå®ä¹ä½äº
build/webpack.base.conf.js
ï¼å¥½å¤å°±æ¯å¼å ¥ä¸éæé½å¾æ¹ä¾¿
ä¾å¦ï¼å¨æç»ä»¶ä¸ï¼å¼å ¥
userService
éè¦import userService from '../../../services/userService'
ä½æäºè·¯å¾å«ååï¼åªéè¦import userService from 'SERVICE/userService'
ç¸æ¯äº AngularJS ä¸çä¾èµæ³¨å ¥ï¼è¿ç§æ¹å¼ä¾èµäºæå»ºå·¥å ·ï¼æ¾å¾æ´ä¸ºç®åæ¨å¯è½ä¼è¯´ï¼Webpack åªéè¦è®¾å®äº
root
å±æ§ä¸ºsrc/
å°±å¯ä»¥import userService from 'services/userService'
ä½å¨è¿éå ¶å®æ¯ä¼å¼èµ·æ§ä¹çï¼ä¸è¿è¿å±äºå¼ºè¿«ççèç´ãããï¼
ä¾å¦ï¼import createBrowserHistory from 'history/lib/createBrowserHistory'
æ¨å¯è½ä¼è§å¾è¿æ¯src/history/lib/createBrowserHistory.js
ä½å®é ä¸ history æ¯ä¸ä¸ª npm package
åæ ·å°ï¼æ¨åæä¹ç¥éservices
ä¸æ¯ä¸ä¸ª npm packageï¼
èä¸éæä¹åï¼æ件夹çåå¨ä¼å¯¼è´ç¸å¯¹è·¯å¾çååï¼services/
ç®å½æªå¿ ä»å¨src/
ä¸
å æ¤ï¼è·¯å¾å«åç¸å½æå¿ è¦ãå ¶å¸¸éçå½¢å¼ï¼è®©äººä¸çå°±ç¥éä¸æ¯ä¸ä¸ª npm package
- å¼åç¯å¢å
¨å±åéï¼ç±
webpack.DefinePlugin
æä¾ï¼è¯¦è§build/webpack.base.conf.js
ï¼
é»è®¤æ
__DEV__
/__PROD__
/__COMPONENT_DEVTOOLS__
/__WHY_DID_YOU_UPDATE__
åä¸ªå ¨å±åé
è¥è¦ç»§ç»æ·»å ï¼åè¿éè¦å¨.eslintrc
ä¸globals
åæ¥åå ¥å¨æ¤éè¦æéï¼å¨
package.json
ä¸è®¾ç½®NODE_ENV
è¦æ³¨ææ«å°¾ç©ºæ ¼çé®é¢
æ好就æ¯ä½¿ç¨åtrim
ä¸ä¸ï¼process.env.NODE_ENV.trim()
æå±é 读ï¼è§£è¯» UglifyJS
ççç产ç¯å¢ä¸ç¼è¯if (__PROD__) { ... }
=>if (true) { ... }
å UglifyJS ä¼å¦ä½å¤ç
â è§è
æ¬ç¤ºä¾é¡¹ç®ç代ç æ尽详ç»å°æ·»å äºæ³¨éï¼å ¶ä¸ä¸ä¹æä½³å®è·µæ示
为äºåå°ä»£ç éï¼æçå»äº Prop éªè¯ï¼å»ºè®®æ¨å¨å¾åçå¼åä¸ä½¿ç¨
â æ§è½
大æ¦å¯åé å¦ä¸æç« ï¼
- React ææ¡£ · Advanced Performance
- å鸡汤 · Should I use shouldComponentUpdateï¼è¯æï¼
- æ·å® FED · é«æ§è½ React ç»ä»¶
- è ¾è®¯ Dev Club · React ç§»å¨ Web æè´ä¼å
§ æµè¯
请èªè¡éæ©æµè¯å·¥å ·
§ é¨ç½²
å¨ react-demo
çå½ä»¤çªå£ä¸ï¼æ²ä¸ npm run build
ï¼å°ä¼å¨é¡¹ç®æ ¹ç®å½ä¸çæ dist/
æ¨å¯ä»¥ä½¿ç¨å½ä»¤è¡éæèµæºæå¡å¨ serve (
npm i serve -g
)ï¼æ²ä¸serve dist/ -p [端å£]
æ¥å¿«éæ¥ç build åç项ç®
è¿å¯ä»¥cd dist
åï¼python -m SimpleHTTPServer [端å£]
æphp -S localhost:[端å£]
å¿«é便æ·å°å®ç°éæèµæºæå¡å¨å ³äºç产ç¯å¢ä¸çé¨ç½²ä¸ä¼åï¼å·²è¶ åºæ¬ææ¡£ç论述èå´ï¼è¯·èªè¡æ¥é ç¸å ³èµæ
§ åè
Top Related Projects
Set up a modern web app by running one command.
A JS library for predictable global state management
Declarative routing for React
🔥 A highly scalable, offline-first foundation with the best developer experience and a focus on performance and best practices.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot