美文网首页
React 初探(九)

React 初探(九)

作者: bowen_wu | 来源:发表于2019-01-22 19:35 被阅读10次

概述

之前已经说了搭建一个 React 项目的基本知识,现在就来正式搭建一个 React 项目。首先说一下这个 React 项目的页面和功能

项目介绍

这个 React 项目主要用于移动端,共有四个页面

  • Login -> 用于登录
  • Home -> 展示主页
  • Detail -> 展示详情页面
  • 404 -> 404 页面
  • Account -> 个人中心页面

使用的库包括

  • react
  • react-redux
  • react-router-dom
  • redux
  • react-sticky
  • rc-form
  • node-sass
  • antd-mobile

使用 fetch 进行 AJAX 请求
使用 scss 进行 css 的编写

快速搭建项目

通过 create-react-app 脚手架快速搭建一个 React 项目

create-react-app react-project
cd react-project

在 github 上创建一个空仓库,创建之后便可以将代码 push 到这个新仓库中,运行这两句话即可。我的项目地址

push 到新仓库

git 相关操作

  1. 创建开发分支并切换到开发分支
git checkout -b dev
  1. 将开发分支提交到 github 上
git push --set-upstream origin dev

目前为止我们便有了两个分支

  • master -> 用于发布正式版本
  • dev -> 用于开发

之后我们便使用 dev 分支进行开发

安装依赖

yarn add -D react-redux
yarn add -D react-router-dom
yarn add -D redux
yarn add -D react-sticky
yarn add -D rc-form
yarn add -D node-sass
yarn add -D antd-mobile
yarn add -D react-app-rewired
yarn add -D babel-plugin-import

或者

yarn add -D react-redux react-router-dom redux react-sticky rc-form node-sass antd-mobile react-app-rewired babel-plugin-import

引入 ant-design-mobile UI 框架

  1. 更改入口文件 index.html。在 public 目录下 index.html 文件的 <head> 标签中加入
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
    <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>
  1. 更改 package.json 文件里的启动配置。
/* package.json */
"scripts": {
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test --env=jsdom",
+   "test": "react-app-rewired test --env=jsdom",
}
  1. 在根目录下创建 config-overrides.js 文件,用于修改默认配置
const { injectBabelPlugin } = require('react-app-rewired');
module.exports = function override(config, env) {
    config = injectBabelPlugin(['import', {
        libraryName: 'antd-mobile',
        style: 'css'
    }], config);
    return config;
};
  1. 按需引用并使用。将 src 目录下的 App.js 文件更改为:
import React, { Component } from 'react';
import { Button } from 'antd-mobile';  // 引入 Button
import logo from './logo.svg';
import './App.css';

class App extends Component {
    render() {
        return (
            <div className="App">
                {/* 使用 Button */}
                <Button>default</Button>
                <Button type="primary">primary</Button>
            </div>
        );
    }
}

export default App;
  1. 运行项目
yarn start
  • 第一个报错


    error

    解决方案:

yarn add -D react-app-rewired@1.6.2
  • 第二个报错


    error

    解决方案:

yarn remove react-scripts
yarn add -D react-scripts@2.1.1

之后便可以运行成功。将浏览器改为手机模式,即可看到两个 button

引入 ant-design-mobile 效果图

配置 SCSS

官网中已经说明如何配置 scss,只需要安装 node-sass npm 包即可,之前我们安装依赖的时候已经安装过了,所以可以直接使用 scss

  1. App.css 改为 App.scss
  2. App.js 中将 import './App.css'; 改为 import './App.scss'; 即可
  3. 更改 App.scss 内容,UI 将自动进行更新

之后我们将使用 scss 编写 css

发布

这里主要说两点有关发布的问题:

  1. github 发布
  2. 上传到测试或者线上

github 发布

github 发布主要是让他人可以通过 github 直接看到我们的项目

  1. 修改 .gitignore 文件,将其中的 /build 删除

  2. 运行:

    yarn build
    

    build 之后仔细看一下 log,我们可以看到

    yarn build log
    我们重点关注一下 homepage。我们需要在 package.json 中添加 homepage 字段,即:
    "homepage" : "http://myname.github.io/myapp/build"
    
    • myname -> github 的 这部分
      myname
    • myapp -> 仓库名
  3. 再次打包并提交到 github 上

  4. 合并到 master 分支

    git checkout master
    git merge dev
    
  5. master 分支打包

    yarn build
    
  6. 提交到 github 上

  7. 在 github 项目的 settings 中的 GitHub Pages

    GitHub Pages
    之后点击保存,页面刷新后将 GitHub PagesUrl copy 下来
    GitHub Pages Url
  8. 在项目中的描述中添加预览链接,在刚刚 copy 的 Url 后面添加 build/index.html 即可

上传到测试或线上

上传到测试或线上将打包的代码上传到服务器上,我主要使用脚本进行上传。

  1. 创建 update.sh 脚本
  2. 内容为
    # 说明:
    # 将此文件放置于默认bash目录,直接运行
    # 发测试环境 source update.sh dev
    # 发正式环境 source update.sh pro
    
    if [ $1x = "pro"x ]; then
      rsync -avz ./build/* MyName@Ip:Path
      echo "production code publish success"
    elif [ $1x = "dev"x ]; then
      rsync -avz ./build/* MyName@Ip:Path
      echo "develop code publish success"
    else
       echo "hello world"
    fi
    
  3. 上传到服务器
    • 发测试环境
      source update.sh dev
      
    • 发线上环境
      source update.sh pro
      

相关文章

  • React 初探(九)

    概述 之前已经说了搭建一个 React 项目的基本知识,现在就来正式搭建一个 React 项目。首先说一下这个 R...

  • React Native初探(三)- Mac

    在React Native初探(一) - Mac和React Native初探(二)- Mac中,很简陋但是能用的...

  • React 初探

    原文地址 React 初探 [1.React 简单介绍](https://github.com/laispace/...

  • 探索React源码:Reconciler

    在探索React源码:初探React fiber[https://juejin.cn/post/703562827...

  • (React启蒙)初探React

    初探React 本文翻译自Cody Lindley的《React Enlightenmen》,已获得作者授权,这套...

  • React Native 相关

    React Native 初探 https://www.cnblogs.com/yexiaochai/p/6042...

  • umi框架的使用

    介绍umi umi官方文档 初探 对比以往使用的 create-react-app 搭建react项目,根据需要我...

  • 2018-05-23

    Lottie Android 初探 Lottie是一个支持Android、iOS、React Native,并由 ...

  • Lottie Android 初探

    Lottie Android 初探 Lottie是一个支持Android、iOS、React Native,并由 ...

  • React初探

    1.基本开发环境 选择 reate-react-app或 create-react-app-antd 2.搭建路由...

网友评论

      本文标题:React 初探(九)

      本文链接:https://www.haomeiwen.com/subject/ejgcjqtx.html