文章目录

最近在学习使用React作为前端的框架,《React使用笔记》系列用于记录过程中的一些使用和解决方法。
本文记录搭建登录页面的过程。

根据产品规划划分模块


主要页面逻辑

在这里,本骚年就建一个比较简单的项目。该项目与之前的Angular使用笔记项目长得完全一致,但我们这里用React来实现吧。

  • 我们的主要页面逻辑如下:
    • 1.登录页面,输入账号和密码即可
    • 2.模块页面

创建登录页面

  • 首先我们在components文件夹内添加一个login.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import React from 'react';
const Login = React.createClass({
render() {
return (
<div className="container" id="login">
<form id="login-form">
<h3 className="text-center">login</h3>
<div className="form-group">
<label>account</label>
<input type="text" className="form-control" placeholder="Account" ref="loginName" required />
</div>
<div className="form-group">
<label>Password</label>
<input type="password" className="form-control" placeholder="Password" ref="loginPwd" required />
</div>
<button type="submit" className="btn btn-default" onClick={this.loginSubmit}>登录</button>
</form>
</div>
)
}
});
module.exports = Login;
  • 在jsx中,因为js中class为保留字,所以要写成className
  • 此处引用了Bootstrap的样式,在templates/index.ejs中添加
1
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">

React Router


安装使用

  • 通过npm安装
1
$ npm install history react-router@latest
  • 还需要安装history,它也是React Router的依赖,且在npm 3+下不会自动安装
1
$ npm install --save history
  • 添加Route组件
1
import { Router, Route, Link, hashHistory, IndexRoute } from 'react-router';

React Router组件

  • Router组件
    Router组件本身只是一个容器,真正的路由要通过Route组件定义。
  • Route组件
    Route组件还可以嵌套。

    1
    2
    3
    4
    5
    <Router history={hashHistory}>
    <Route path="/" component={App}>
    <Route path="/repos" component={Repos}/>
    </Route>
    </Router>
  • Link组件
    Link组件用于取代元素,生成一个链接,允许用户点击后跳转到另一个路由,可接收Router的状态。

  • IndexLink组件
    如果链接到根路由/,不要使用Link组件,而要使用IndexLink组件。
  • IndexRoute组件
    IndexRoute显式指定Home是根路由的子组件,即指定默认情况下加载的子组件,即该路径的index.html。
  • Redirect组件
    Redirect组件用于路由的跳转,即用户访问一个路由,会自动跳转到另一个路由。
  • IndexRedirect组件
    IndexRedirect组件用于访问根路由的时候,将用户重定向到某个子组件。

path属性

Route组件的path属性指定路由的匹配规则。
path属性可以使用相对路径(不以/开头),匹配时就会相对于父组件的路径。

  • :paramName
    • 匹配URL的一个部分,直到遇到下一个/、?、#为止。
    • 这个路径参数可以通过this.props.params.paramName取出。
  • ()
    • ()表示URL的这个部分是可选的。

  • 匹配任意字符,直到模式里面的下一个字符为止。匹配方式是非贪婪模式。

**匹配任意字符,直到下一个/、?、#为止。匹配方式是贪婪模式。

Histories

React Router是建立在history之上的。 简而言之,一个history知道如何去监听浏览器地址栏的变化,并解析这个URL转化为location对象,然后router使用它匹配到路由,最后正确地渲染对应的组件。

  • createHashHistory
    • 这是一个你会获取到的默认history,如果你不指定某个history(即 {/ your routes /})。
    • 它用到的是URL 的hash(#)部分去创建形如examp 大专栏  React使用笔记2--创建登录组件le.com/#/some/path的路由。
    • ?_k=ckuvup是每一个location创建的一个唯一的key,并把它们的状态存储在session storage中。当访客点击“后退”和“前进”时,我们就会有一个机制去恢复这些location state。可使用queryKey: false关闭。
  • createBrowserHistory
    • Browser history使用History API在浏览器中被创建用于处理URL,新建一个像这样真实的URL example.com/some/path。
    • 使用Browser history需要在服务器进行配置。
  • createMemoryHistory
    • Memory history不会在地址栏被操作或读取。

js中设置跳转

  • 使用browserHistory.push
1
2
3
4
import { browserHistory } from 'react-router';
example(event) {
browserHistory.push(path);
}
  • 使用context对象
1
2
3
4
5
6
7
8
9
export example React.createClass({
contextTypes: {
router: React.PropTypes.object
},
example(event) {
this.context.router.push(path)
},
})

添加路由


在index.js设置路由

1
2
3
4
5
6
7
8
9
10
11
12
13
import { Router, Route, Link, hashHistory, IndexRoute, useRouterHistory } from 'react-router'; //router组件
import { createHistory, createHashHistory } from 'history'; //history组件
import Login from './components/login.jsx'; //login自定义组件
import Index from './components/index.jsx'; //index自定义组件
let history = useRouterHistory(createHashHistory)({ queryKey: false });
//将其渲染到页面上id为test的DOM元素内
ReactDOM.render(<Router history={history}>
<Route path="/">
<Route path="index" component={Index} />
<IndexRoute component={Login} />
</Route>
</Router>,
document.body);

在components里login.ejs添加路由跳转

  • 添加登录按钮的click事件
  • 添加loginSubmit属性以及跳转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import React from 'react'; //导入react组件
const Login = React.createClass({
contextTypes: {
router: React.PropTypes.object
},
loginSubmit: function() {
this.context.router.push('/index'); //使用this.content进行跳转
},
render() {
return (
<div className="container" id="login">
<form id="login-form">
<h3 className="text-center">login</h3>
<div className="form-group">
<label>account</label>
<input type="text" className="form-control" placeholder="Account" ref="loginName" required />
</div>
<div className="form-group">
<label>Password</label>
<input type="password" className="form-control" placeholder="Password" ref="loginPwd" required />
</div>
<button type="submit" className="btn btn-default" onClick={this.loginSubmit}>登录</button>
</form>
</div>
)
}
});
module.exports = Login;

结束语


从Angular转React中遇到不少问题呢,毕竟两者很多概念和使用方法都很不一样,使用过程中也是大开眼界了呀。
此处查看项目代码(仅包含app部分)
此处查看页面效果

查看Github有更多内容噢:https://github.com/godbasin

更欢迎来被删的前端游乐场边撸猫边学前端噢

React使用笔记2--创建登录组件的更多相关文章

  1. react学习笔记1之声明组件的两种方式

    //定义组件有两种方式,函数和类 function Welcome(props) { return <h1>Hello, {props.name}</h1>; } class ...

  2. Yii2.0学习笔记:创建登录表单

    第一步:在model层创建一个EntryForm.php文件 复制以下代码,注意model的文件.方法.类的命名规范(大小写) <?php namespace app\models; use Y ...

  3. React学习笔记 - 组件&Props

    React Learn Note 4 React学习笔记(四) 标签(空格分隔): React JavaScript 三.组件&Props 组件可以将UI切分成一些独立的.可复用的部件,这样你 ...

  4. React学习笔记 - Hello World

    React Learn Note 1 React学习笔记(一) 标签(空格分隔): React JavaScript 前.Hello World 1. 创建单页面应用 使用Create React A ...

  5. React学习笔记 - 元素渲染

    React Learn Note 3 React学习笔记(三) 标签(空格分隔): React JavaScript 二.元素渲染 元素是构成react应用的最小单位. 元素是普通的对象. 元素是构成 ...

  6. React学习笔记 - JSX简介

    React Learn Note 2 React学习笔记(二) 标签(空格分隔): React JavaScript 一.JSX简介 像const element = <h1>Hello ...

  7. 《React后台管理系统实战 :一》:目录结构、引入antd、引入路由、写login页面、使用antd的form登录组件、form前台验证、高阶函数/组件

    实战 上接,笔记:https://blog.csdn.net/u010132177/article/details/104150177 https://gitee.com/pasaulis/react ...

  8. 理解React中es6方法创建组件的this

    首发于:https://mingjiezhang.github.io/(转载请说明此出处). 在JavaScript中,this对象是运行时基于函数的执行环境(也就是上下文)绑定的. 从react中的 ...

  9. react系列笔记1 用npx npm命令创建react app

    react系列笔记1 用npx npm命令创建react app create-react-app my-app是开始构建新的 React 单页应用程序的最佳方式.它已经为你设置好了开发环境,以便您可 ...

随机推荐

  1. python妹子图爬虫5千张高清大图突破防盗链福利5千张福利高清大图

    meizitu-spider python通用爬虫-绕过防盗链爬取妹子图 这是一只小巧方便,强大的爬虫,由python编写 所需的库有 requests BeautifulSoup os lxml 伪 ...

  2. 蓝桥杯 Car的旅行路线 (预处理+最短路径)

    https://www.luogu.org/problem/P1027 题目描述 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游.她知道每个城市都有4个飞机场,分别位于一个矩形的4个顶点上,同 ...

  3. log 处理

    保存文件, 并打印到屏幕 import logging def set_log(log_file='log.log'): logging.basicConfig( level = logging.IN ...

  4. day53-线程池

    #1.from concurrent import futures可以开启进程池和线程池.concurrent是包,futures是模块,ThreadPoolExecutor是类,submit是方法. ...

  5. saltstack的salt-api介绍

    一.salt-api安装 yum install salt-api pyOpenSSL -y #pyOpenSSL 生成自签证书时使用 二.生成自签名证书(ssl使用) [root@master ce ...

  6. 爬虫笔记(九)——安装Fiddler

    在ubuntu下不能直接安装Fiddler,我们要先安装mono环境,具体可分为三个步骤: 1.   在终端下输入指令安装mono环境 :sudo apt-get install mono-compl ...

  7. [SDOI2019]染色(DP)

    好神的题啊! 看了这题只会第一个subtask,又参考了HN-CJ鸽王zsy的题解,实在太菜了. 暴力转移是O(nc2),很显然没有分.考虑子任务1,2,只需要转移包含已染色格子的列,然后状态数只有O ...

  8. MySQL报错解决:The MySQL server is running with the --read-only option so it cannot execute this statement

    MySQL报错:The MySQL server is running with the --skip-grant-tables option so it cannot execute this st ...

  9. vue项目打包部署elementUI的字体图标丢失问题

    自己搭建的Vue项目,没有使用vue-cli,引入elementUI时,使用的是webpack直接打包工具,发现字体图标丢失你 记录一下解决办法: webpack module配置:(build目录下 ...

  10. 写入简单的日志log

    log.c: #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h& ...