文章目录

最近在学习使用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. Prometheus监控系统之入门篇(一)

    1. 简介 Prometheus: (简称Prom)是由SoundCloud开发的开源监控报警系统.是大名鼎鼎的CNCF云原生基金会下的第二大开源项目.具有如下特点: 使用Go语言开发 内置时序数据库 ...

  2. python学习——函数及其参数

    函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段.函数能提高应用的模块性,和代码的重复利用率.严格来说python只有函数,没有过程,人们理解的函数都是带有return的,而过程 ...

  3. Okhttp教程 (1)

    1. 在build.gradle里引入okhttp库 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testComp ...

  4. MySql 按日期条件查询数据

    本周内: select * from wap_content where week(created_at) = week(now) 查询一天: select * from table where to ...

  5. 共克时艰,停工不停学 Serverless 在线课堂免费开课

    二月份,Serverless 团队联合腾讯云大学与云+社区免费推出 Serverless 在线直播课程,课程涵盖 Serverless 架构解密.技术解析以及不同应用场景下的最佳实战指导,让你从 0 ...

  6. OA项目-需求分析

    ###############  需求分析   ############### """ 工作流 1,工单管理 2,执行记录 权限管理 1,菜单 2,角色, 用户管理 1, ...

  7. Cisco路由器配置基本命令

    特权模式:enable Router1 # show running-config Router1 # show ip route Router # show ip interface brief S ...

  8. python之urllib模块和requests模块

    一.urllib模块 python标准库自带的发送网络请求的模块. # 用python怎么打开浏览器,发送接口请求 import urllib from urllib.request import u ...

  9. 概率DP——BZOJ4008 [HNOI2015]亚瑟王

    [HNOI2015]亚瑟王 Description 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑.他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂 ...

  10. Contiguous Repainting

    题目描述 There are N squares aligned in a row. The i-th square from the left contains an integer ai. Ini ...