文章目录

最近在学习使用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笔记_第二篇_面向过程_第二部分_3.模块的概述

    这部分内容是非常重要的,分模块的基本概念和一些常用模块的使用,其实常用模块使用这部分也不是太全面,后续或者有机会再通过其他材料进行讲解. 1. 模块的概述: 目前代码比较少,写在一个文件中还体现不出什 ...

  2. 关于汽车诊断OBD的理解(ISO15031-5)(转发)

    1.OBD用来做什么 2.OBD和UDS的区别 3.OBD硬件接口简介 4.OBD的9大模式介绍 OBD(On-Board Diagnostic)指的是在线诊断系统,是汽车上的一种用于监控车辆状况以及 ...

  3. Windows 常用配置 - 启用长路径

    Windows 启用长路径支持 打开注册表编辑器:regedit 找到如下路径:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSyte ...

  4. 字符串常用方法总结与StringBuffer基础

    字符串 基本特性 final:字符串被修饰为final,是不能被继承的. immutable:字符串是不可改变的,例如创建了一个字符串对象,就不可改变它,即不能进行增删查改其中的字符.一旦创建好这个字 ...

  5. ModelSerializer补充及ListSerializer

    整体单改 路由层.模型层.序列化层不需要做修改,只需要处理视图层:views.py """ 1) 单整体改,说明前台要提供修改的数据,那么数据就需要校验,校验的数据应该在 ...

  6. day17-反射

    #反射最常用的两个方法:hasattr getattr # 1. 反射对象属性,反射对象方法: class Goods: def __init__(self,name): self.name = na ...

  7. python语法基础-面向对象-基础-长期维护

    ###############    类的基本操作    ############## """ 类的基本认识: 1,类就是一个模子 2,dict,list都是类,具体的一 ...

  8. 3dmax2018卸载/安装失败/如何彻底卸载清除干净3dmax2018注册表和文件的方法

    3dmax2018提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装3dmax2018失败提示3dmax2018安装未完成,某些产品无法安装,也有时候想重新安装3 ...

  9. python循环删除list中的元素

    直接上例子: a = [1,2,3,4,5,6] for i in a: a.remove(i) print(a) 返回:[2, 4, 6] 循环a,想删除a的所有元素,但实际确有数据保留了下来,这是 ...

  10. IO流文件拷贝

    目录 IO流文件拷贝 前言 字节流(使用FileInputStream和FileOutputStream读取每一个字节...) 字节流(使用FileInputStream和FileOutputStre ...