zoukankan      html  css  js  c++  java
  • 初始化 react 项目

    初始化一个 react 项目

    记一个完整的 react + antd-mobile + redux

    1. 下载脚手架工具 npm install -g create-react-app
    2. 初始化 react 项目:
      create-react-app project-name
    3. 测试运行:
    • 载入刚刚下载的目录:cd project-name
    • 运行: npm start
    • 查看结果: localhost:3000
    1. 编码测试与打包发布项目
    • 编码测试:
    • 打包发布
    • npm run build
    • 全局安装npm install -g serve
    • serve build
    • 访问网址: http://localhost:5000
    • 如果不想使用 serve build 启动打包好的项目,也可以自定义 package.json文件的scripts, 如:
    "scripts": {
        "prod": "serve build"
     }
    

    这样子就可以通过 npm prod启动打包好的项目

    1. 项目目录设计
    - node_modules
    - public
    |-- favicon.icon
    |-- index.html
    - src
    |-- api
    |-- assets
    |-- components
    |-- containers
    |-- redux
        |-- actions.js
        |-- actions-type.js
        |-- reducers.js
        |-- store.js
    |-- utils
    - index.js
    - .gitignore
    - package.json
    - package-lock.json
    - README.md
    - yarn.lock
    
    1. 引入antd-mobile
    • 下载: npm install antd-mobile --save
    • 页面处理: index.html
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
        <meta name="theme-color" content="#000000" />
        <meta
          name="description"
          content="Web site created using create-react-app"
        />
        <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
        <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
        <title>React App</title>
        <script src="https://as.alipayobjects.com/g/component/fastclick/1.0.6/fastclick.js"></script>
        <script>
          if ('addEventListener' in document) {
            document.addEventListener('DOMContentLoaded', function() {
              FastClick.attach(document.body);
            }, false);
          }
          if(!window.Promise) {
            document.writeln('<script src="https://as.alipayobjects.com/g/component/es6-promise/3.2.2/es6-promise.min.js"'+'>'+'<'+'/'+'script>');
          }
        </script>
      </head>
      <body>
        <noscript>You need to enable JavaScript to run this app.</noscript>
        <div id="root"></div>
      </body>
    </html>
    
    1. 引入路由 react-router-dom
      npm install --save react-router-dom

    2. 引入redux

    npm install --save redux@3.7.2 react-redux redux-thunk

    npm install --save-dev redux-devtools-extension


    整合之后代码如下
    创建路由组件:

    containers/Register.jsx

    import React, {Component} from 'react'
    
    export default class Register extends Component {
      render() {
        return (
          <div>
            Register
          </div>
        )
      }
    }
    

    containers/Login.jsx

    import React, {Component} from 'react'
    
    export default class Login extends Component {
      render() {
        return (
          <div>
            Login
          </div>
        )
      }
    }
    

    containers/main.js

    import React, {Component} from 'react'
    
    export default class Main extends Component {
      render() {
        return (
          <div>
            Main
          </div>
        )
      }
    }
    

    redux/reducers.js

    /*
     包含多个 reducer 函数, 根据旧的 state 和指定的 action 返回一个 新的 state
     */
    
    import { combineReducers } from 'redux'
    
    function XXX(state = 0, action) {
      return state
    }
    
    function YYY(state = 0, action) {
      return state
    }
    
    export default combineReducers({
      XXX,
      YYY
    })
    

    redux/store.js

    /*
      redux 最核心的管理对象模块
     */
    
    import { createStore, applyMiddleware } from 'redux'
    import thunk from 'redux-thunk'
    import { composeWithDevTools } from 'redux-devtools-extension'
    
    import reducers from './reducers'
    
    // 向外暴露 store
    export default createStore(reducers, composeWithDevTools(applyMiddleware(thunk)))
    
    

    index.js

    import React from 'react'
    import ReactDOM from  'react-dom'
    
    // 路由
    import { HashRouter, Route, Switch } from 'react-router-dom'
    import Register from './containers/register/register'
    import Login from './containers/login/login';
    
    // store
    import { Provider } from 'react-redux'
    import store from './redux/store';
    
    
    ReactDOM.render((
      <Provider store={ store }>
        <HashRouter>
          <Switch>
            <Route path='/register' component={ Register }></Route>
            <Route path='/login' component={ Login }></Route>
            <Route component={ Main }></Route>
          </Switch>
        </HashRouter>
      </Provider>
    ), document.getElementById('root'))
    
    

    index.html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
        <meta name="theme-color" content="#000000" />
        <meta
          name="description"
          content="Web site created using create-react-app"
        />
        <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
        <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
        <title>React App</title>
        <script src="https://as.alipayobjects.com/g/component/fastclick/1.0.6/fastclick.js"></script>
        <script>
          if ('addEventListener' in document) {
            document.addEventListener('DOMContentLoaded', function() {
              FastClick.attach(document.body);
            }, false);
          }
          if(!window.Promise) {
            document.writeln('<script src="https://as.alipayobjects.com/g/component/es6-promise/3.2.2/es6-promise.min.js"'+'>'+'<'+'/'+'script>');
          }
        </script>
      </head>
      <body>
        <noscript>You need to enable JavaScript to run this app.</noscript>
        <div id="root"></div>
      </body>
    </html>
    
    
  • 相关阅读:
    轻松实现WCF服务的构造函数依赖注入
    终于找到在Visual Studio 2010中进行“项目重命名”的有效工具
    让Entity Framework不再私闯sys.databases
    AutoMapper使用笔记
    遭遇IE8下的JavaScript兼容问题
    WCF异步调用中客户端关闭带来的性能问题
    Chrome “False Start” 引起的 Error 7 (net::ERR_TIMED_OUT): The operation timed out
    实战ASP.NET访问共享文件夹(含详细操作步骤)
    Entity Framework 理清关系 基于外键关联的单向一对一关系
    在Firefox中通过JavaScript复制到剪贴板(Copy to Clipboard)
  • 原文地址:https://www.cnblogs.com/serahuli/p/13570085.html
Copyright © 2011-2022 走看看