Git Product home page Git Product logo

react-practice's Introduction

react-practice

搭建环境

安装

npm init -y

npm i webpack webpack-cli webpack-dev-server -D

npm i @babel/plugin-proposal-class-properties -D

npm i html-webpack-plugin -D

npm i @babel/core babel-loader @babel/plugin-transform-runtime @babel/preset-env @babel/preset-react -D

npm install react react-dom @babel/runtime -S

// AntD 按需加载
npm i babel-plugin-import -D

npm i antd -S

样式和图片

npm i style-loader css-loader -D

npm i node-sass sass-loader -D

npm i file-loader url-loader -D

配置

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

const htmlPlugin = new HtmlWebpackPlugin({
    template: path.join(__dirname, './src/index.html'),
    filename: 'index.html'
});

module.exports = {
    mode: 'development',
    devServer: {
        compress: true,
        port: 3000
    },
    plugins: [
        htmlPlugin
    ],
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                use: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.ttf|woff|woff2|eot|svg|jpg|png|gif|bmp$/i,
                use: 'url-loader'
            },
            {
                test: /\.scss$/,
                use: [
                    'style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            modules: {
                                mode: 'local',
                                localIdentName: '[path][name]__[local]--[hash:base64:5]',
                            }
                        }
                    },
                    'sass-loader'
                ]
            }
        ]
    }
};

目录结构

tree -l 4 --ignore node_modules,dist

├── package-lock.json
├── package.json
├── README.md
├── src
|  ├── components
|  |  ├── about   => 关于
|  |  |  └── index.jsx
|  |  ├── App.jsx => 根组件
|  |  ├── common  => 公共组件
|  |  |  ├── footer
|  |  |  |  └── index.jsx
|  |  |  └── header
|  |  |     ├── index.jsx
|  |  |     └── style.scss
|  |  ├── home    => 首页
|  |  |  └── index.jsx
|  |  ├── movie   => 电影
|  |  |  ├── content.jsx
|  |  |  ├── detail.jsx
|  |  |  ├── index.jsx
|  |  |  └── style.scss
|  |  └── style.css => 全局样式
|  ├── index.html
|  └── index.js => 入口文件
└── webpack.config.js

基本布局

  • 上:header
    • 首页的内容
    • 电影的内容
      • 左:电影类型
      • 右:对应列表
    • 关于
  • 下:footer

路由设计

安装

npm i react-router-dom -S

根据 Hash 高亮当前项

// 刷新高亮当前标题
<Menu defaultSelectedKeys={[location.hash.split('/')[1] || 'home']}></Menu>

电影内页路由

// 注意 exact
<Switch>
    <Route exact path={`${path}`} render={() => <Redirect to={`${path}/in_theaters/1`} />} />
    <Route path={`${path}/detail/:id`} component={Detail} />
    <Route path={`${path}/:type/:pnum`} component={PContent} />
</Switch>

获取数据

豆瓣接口

  1. 根路径:https://api.douban.com
  2. 正在热映:/v2/movie/in_theaters?start=0&count=1
  3. 即将上映:/v2/movie/coming_soon?start=0&count=1
  4. 排行榜:/v2/movie/top250?start=0&count=1
  5. 电影详情:/v2/movie/subject/26861685

解决跨域

// 解决跨域
npm i fetch-jsonp -S

全局配置

// 挂载到入口或根组件上
import fetchJSONP from 'fetch-jsonp';
Component.prototype.$http = fetchJSONP;
Component.prototype.baseURL = 'https://api.douban.com';
Component.prototype.apikey = '0df993c66c0c636e29ecbb5344252a4a';

电影分页

  1. 点击分页
  2. 编程式导航
this.props.history.push(`/movie/${type}/${pnum}`);
  1. 触发 componentWillReceiveProps
  2. 获取数据 getMovieData()

电影详情

注意容错处理

react-practice's People

Contributors

ifer-itcast avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.