React 實踐項目 (一)

React在Github上已經有接近70000的 star 數了,是目前最熱門的前端框架。而我學習React也有一段時間了,如今就開始用 React+Redux 進行實戰!css

文章地址:https://github.com/DigAg/diga...
項目代碼地址:https://github.com/DigAg/diga...html

  • 首先,咱們開始構建一個基礎項目。

咱們使用create-react-app建立項目,不須要安裝或配置W​​ebpack或Babel等工具。它們被預先配置和隱藏,以便咱們能夠專一於代碼。前端

  • 在本地全局安裝create-react-app(須要安裝Node.js且版本 >= 6,也可以使用 yarn 代替 npm)vue

npm install -g create-react-app
  • 建立項目node

create-react-app digag 
cd digag
  • 檢查是否成功建立digag文件夾與相關文件react

digag
├── README.md
├── node_modules
├── package.json
├── .gitignore
├── public
│   └── favicon.ico
│   └── index.html
│   └── manifest.json
└── src
    └── App.css
    └── App.js
    └── App.test.js
    └── index.css
    └── index.js
    └── logo.svg
    └── registerServiceWorker.js
  • 在開發模式下運行應用程序,訪問localhost:3000在瀏覽器中查看。git

npm start 
or 
yarn start
這樣咱們就成功建立好一個能夠直接運行的React項目了!
  • 接下來,開始編寫代碼了!

  • 首先咱們打開 src 目錄下的 App.js 文件,刪除掉默認生成的代碼。輸入如下代碼:github

/**
 * Created by Yuicon on 2017/6/25.
 */
import React, { Component } from 'react';
import Header from "../../components/Index/Header";
import './App.css';

export default class App extends Component {

  componentDidMount() {
    console.log(this.props.users)
  }

  render(){
    return(
      <div className="App">
        <div className="App-header">
          <Header/>
        </div>
        <div className="App-body">
          <div className="welcome-view">
            <div className="category-nav">
              <div>1adasdasdasdasdasd1adasdasdasdasdasd1adasdasdasdasdasd1adasdasdasdasdasd</div>
            </div>
            <div className="main">
              21adasdasdasdasdasd1adasdasdasdasdasd1adasdasdasdasdasd1adasdasdasdasdasd1adasdasdasdasdasd
            </div>
            <div className="sidebar">
              31adasdasdasdasdasd1adasdasdasdasdasd1adasdasdasdasdasd1adasdasdasdasdasd1adasdasdasdasdasd
            </div>
          </div>
        </div>
      </div>
    )
  }
}

一樣,編輯 App.css 文件:npm

html {
  font-size: 12px;
  font-family: -apple-system,PingFang SC,Hiragino Sans GB,Arial,Microsoft YaHei,Helvetica Neue,sans-serif;
  text-rendering: optimizeLegibility;
  background-color: #f4f5f5;
  color: #333;
  word-break: break-all;
}

.App {
  text-align: center;
}

.App-header {
  position: relative;
  height: 5rem;
}

.main-header {
  background: #fff;
  border-bottom: 1px solid #f1f1f1;
  color: #909090;
  height: 5rem;
  z-index: 250;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  transition: all .2s;
}

.main-header .visible {
  transform: translateZ(0);
}

.container {
  display: flex;
  align-items: center;
  height: 100%;
  position: relative;
  width: 100%;
}

.main-header .container {
  max-width: 960px;
  min-width: 960px;
  margin: auto;
}

.logo {
  margin-right: 2rem;
}

.logo-img {
  border-style: none;
}

.nav-menu ul{
  background-color: white;
}

.nav-menu ul li{
  font-size: 1.33rem;
}

.nav-menu ul li:hover{
  border-bottom: 0 solid white !important;
  background-color: white !important;
}

.nav-menu button{
  margin-left: 0 !important;
  font-weight: 500;
  font-size: 1.3rem;
}

.contribute {

}

.contribute:after{
  content: "|";
  position: absolute;
  top: 24px;
  left: 100%;
  color: hsla(0,0%,59%,.4);
}

.login-btn {

}

.login-btn:after {
  content: "\B7";
  margin: 0 .4rem;
}

.register-dialog {
  padding: 2rem;
  width: 26.5rem !important;
  max-width: 100%;
  font-size: 1.167rem;
  box-sizing: border-box;
}

.App-body {
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: 960px;
  height: 100vh;
}

.welcome-view {
  position: relative;
  display: flex;
  justify-content: space-between;
  margin-top: 1.767rem;
}

.category-nav {
  background-color: #db1f00;
  width: 140px;
  position: fixed;
  top: 6.66rem;
}

.main {
  background-color: #08c6a7;
  width: 560px;
  margin-left: 13rem;
}

.sidebar {
  background-color: #e3e001;
  width: 19.2rem;
  box-sizing: border-box;
}

可能已經有同窗注意到了,咱們在 App.js 裏導入了一個目前還不存在的組件。如今,咱們來建立它:json

  • 首先,建立 src/components/Index 目錄,在該目錄下建立 Header.js

digag
├── README.md
├── node_modules
├── package.json
├── .gitignore
├── public
│   └── favicon.ico
│   └── index.html
│   └── manifest.json
└── src
    └── components
        └── Index
            └── Header.js
    └── App.css
    └── App.js
    └── App.test.js
    └── index.css
    └── index.js
    └── logo.svg
    └── registerServiceWorker.js

編輯 Header.js 文件

/**
 * Created by Yuicon on 2017/6/25.
 */
import React, {Component} from 'react';
import {Button, Input, Menu} from "element-react";

export default class Header extends Component {

  constructor(props) {
    super(props);
    this.state = {
      searchInput: '',
    };
  }

  handleSelect = (index) => {
    console.log(index);
  };

  handleIconClick = () => {
    console.log('handleIconClick', this.state.searchInput);
  };

  render() {
    return (
      <header className="main-header visible">
        <div className="container">
          <a href="/" className="logo">
            <img src="//gold-cdn.xitu.io/v3/static/img/logo.a7995ad.svg" alt="掘金" className="logo-img"/>
          </a>
          <div className="nav-menu">
            <Menu defaultActive="1" mode="horizontal" onSelect={this.handleSelect}>
              <Menu.Item index="1">首頁</Menu.Item>
              <Menu.Item index="2">專欄</Menu.Item>
              <Menu.Item index="3">收藏集</Menu.Item>
              <Menu.Item index="4">發現</Menu.Item>
              <Menu.Item index="5">標籤</Menu.Item>
              <Menu.Item index="6">
                <Input
                  size="small"
                  icon="search"
                  placeholder="搜索掘金"
                  onIconClick={this.handleIconClick}
                  onChange={(value) => this.setState({searchInput: value})}
                />
              </Menu.Item>
              <Menu.Item index="7">
                <Button type="text" icon="edit" className="contribute">投稿</Button>
              </Menu.Item>
              <Menu.Item index="8">
                <Button type="text" className="login-btn"
                        onClick={ () => console.log('登陸') }>登陸</Button>
                <Button type="text" onClick={ () => console.log('註冊') }>註冊</Button>
              </Menu.Item>
            </Menu>
          </div>
        </div>
      </header>
    )
  }
}
  • 咱們在 Header.js 文件裏導入了 element-react UI庫的組件, 因此須要在 package.json 文件裏添加依賴。

//省略部分代碼
"dependencies": {
     "element-react": "^1.0.11",
     "element-theme-default": "^1.3.7"
    },

運行命令:

npm install 
or 
yarn install

根據 element-react 文檔,在 index.js 文件中導入樣式

import 'element-theme-default';
//省略部分代碼
  • 如今再從新運行項目,咱們能夠看到這樣的頁面:

index

是的沒錯,這就是編寫一個屬於本身的掘金教程。

下篇教程會加入 Redux 進行登陸註冊操做

項目代碼地址:https://github.com/DigAg/diga...
vue2版項目代碼地址:https://github.com/DigAg/diga...
相應後端項目代碼地址:https://github.com/DigAg/diga...

相關文章
相關標籤/搜索