React初探
经过几天根据官方文档和博园中一些大牛的文章,在了解过基础的语法和组件后,总结一下:
1.第一件事就是分析界面,理想状态下是让每个组件只做一件事情,也就是单一职责,相互嵌套。我认为:
- 构建组件树,整体的构架,把整体的各个组件罗列出来。
- 也可以从小组件开始写,能够清除的知道该组件需要什么数据,就让该组件的父组件只传所需要的数据。
2.我们要知道调用行第一次render方法的时候,声明周期到底执行了哪些方法?
初始化渲染:getDefaultProps
getInitialState
componentWillMount
render
componentDidMount
当然Props或者state改变:
componentWillReceiveProps
shouldComponentUpdata (数据发生改变返回ture执行render)
componentWillUpdate
render
componentDidUpdata
3.尝试了用es6+webpack 写todolist。 (参考了王福朋博客的todolist)
class Text extends React.Component{ constructor(){
super();
this.state={
todolist:[]
}
}
onchange(row){
this.setState({
todolist:row
})
}
render(){ return(
<div>
<TypeNew change={this.onchange.bind(this)} todolist={this.state.todolist}/>
<ListTodo change={this.onchange.bind(this)} todolist={this.state.todolist}/>
</div>
)
}
}
- 首先是最外层的组件,里面包括了TypeNew 输入框,ListType 展示列表。设置了初始状态值为空数组,onchange函数是改变数据时调用。
- 往子组件传递方法和数据通过props。
- 在es6 class语法中,change={this.onchange.bind(this)} 如果不写bind(this)将会找不到该方法,所以需要用bind()指定上下文。当然也可以在constructor()中设置。
class ListTodo extends React.Component{ constructor(props){
super(props); }
handleclick(e){
var deIndex=e.target.getAttribute("data-index")
this.props.todolist.splice(deIndex,);
this.props.change(this.props.todolist)
}
render(){ return(
<ul>
{
this.props.todolist.map(function(item,index){
return <li className="list-border" key={index}>
<label>{item}</label>
<button data-index={index} onClick={this.handleclick.bind(this)}>delete</button>
</li>
}.bind(this))
}
</ul>
)
}
}
- 通过this.props.todolist可以获取到由父组件传下来的数据。
- 添加data-index自定义属性是为了获取点击后获取位置。 我们通过e.target 可以获取点击的元素,再通过getAttribute()获取自定义属性的值。再删除数组中对应位置的数据。
class TypeNew extends React.Component{ constructor(props){
super(props); }
onsubmit(e){
e.preventDefault();
var input=this.refs.myinput;
var text=input.value;
var row= this.props.todolist;
if(text !==""){
row.push(text)
this.props.change(row)
input.value=""
} }
render(){ return(
<form onSubmit={this.onsubmit.bind(this)}>
<input ref="myinput" type="text" placeholder="typing a newthing todo" />
</form>
)
}
}
- 以上我们用从父组件传递下来的change函数通过传递参数的形式改变了父组件的数据。
4.尝试加入了react-router 路由功能。
看了阮一峰老师的react-router文章以及结合了一些实例,算是有所入门。
//main.js
import React from 'react';
import ReactDom from 'react-dom'; import Component1 from './components/Component1.jsx';
import Text from './components/Text.jsx';
import { Menu, Icon } from 'antd';
import "antd/dist/antd.min.css" const SubMenu = Menu.SubMenu;
const MenuItemGroup = Menu.ItemGroup; // 引入React-Router模块
import { Router, Route, Link, hashHistory, IndexRoute, Redirect, IndexLink} from 'react-router'
var i=0;
class App extends React.Component {
constructor(){
super(); } render(){
return (
<div> <div>
<Menu mode="horizontal">
<Menu.Item key="mail">
<Icon type="mail" />Navigat ion One
</Menu.Item>
<Menu.Item key="app" >
<Icon type="appstore" />Navigation Two
</Menu.Item>
<SubMenu title={<span><Icon type="setting" />Navigation Three - Submenu</span>}>
<MenuItemGroup title="Item 1">
<Menu.Item key="setting:1"><IndexLink to="/" activeClassName=”active“ >Option 1</Link></Menu.Item>
<Menu.Item key="setting:2"><Link to="/Component1" activeStyle={{color: 'red'}} >Option 2</Link></Menu.Item>
</MenuItemGroup>
<MenuItemGroup title="Item 2">
<Menu.Item key="setting:3">Option 3</Menu.Item>
<Menu.Item key="setting:4">Option 4</Menu.Item>
</MenuItemGroup>
</SubMenu>
<Menu.Item key="alipay">
<a href="https://ant.design" target="_blank" rel="noopener noreferrer">Navigation Four - Link</a>
</Menu.Item>
</Menu>
</div>
<div>
{ this.props.children }
</div>
</div>
)
}
} ReactDom.render(
(
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Text} />
<Route path="Component1" component={Component1} />
</Route>
</Router>
),
document.getElementById("content")
)
Router 本身就是一个容器 路由功能都需要它来定义。
hashHistory :表示页面的切换是通过url的hash的改变。
path : 参数定义了hash的值。
component :参数定义了访问的模块内容。
以上代码为嵌套路由,当我们访问localhost:8080时,首先会加载App组件(导航栏),无论如何切换页面,该导航栏都会存在。然后才会去加载它的子组件Text。至于为什么是Text?那得依靠IndexRoute 。
IndexRoute :参数表示默认加载的子组件,如果不写,该页面只会展示App组件,不会展示子组件。
嵌套路由时,App组件需要如代码标记处一样用this.props.childer。在切换页面时候将会在该位置展示子组件。
那该如何切换页面呢?Link组件。它取代了a标签,to参数指定了跳转路径,也即是path。该组件还可以添加activeStyle,activeClassName样式,当跳到该页面时 该标签也会改变样式。
就这么多了,任重而道远。
React初探的更多相关文章
- React 初探
React 简单介绍 先说 React 与 React Native 他们是真的亲戚,可不像 Java 和 Javascript 一样. 其实第一次看到 React 的语法我是拒绝的,因为这么丑的写法 ...
- react初探(二)之父子组件通信、封装公共组件
一.前言 在组件方面react和Vue一样的,核心思想玩的就是组件,下面举两个组件常用的情景. 场景一:假如我们现在有一个页面包含表格以及多个弹框,这种时候如果将这个页面的业务代码写在一个组件中,那么 ...
- React学习之一:React初探
一,React简介 React是由Facebook和Instagram开发的一套创建用户界面的JavaScript库.许多人认为React是MVC中的V. React创建的目的是为了:构建数据随时会改 ...
- facebook开源前端UI框架React初探
最近最火的前端UI框架非React莫属了.赶紧找时间了解一下. 项目地址:http://facebook.github.io/react/ 官方的介绍:A JavaScript library for ...
- react初探(一)之JSX、状态(state)管理、条件渲染、事件处理
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技术栈为react,目前我的情况是三大框架只会angular和Vue.在实际项目中只使用过一次angular5,其余项目都是使用Vue写的.写篇 ...
- React Native初探
前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...
- 【优质】React的学习资源
React的学习资源 github 地址: https://github.com/LeuisKen/react-collection https://github.com/reactnativecn/ ...
- React Native指南汇集了各类react-native学习资源、开源App和组件
来自:https://github.com/ele828/react-native-guide React Native指南汇集了各类react-native学习资源.开源App和组件 React-N ...
- 简单理解ECMAScript2015中的箭头函数新特性
箭头函数(Arrow functions),是ECMAScript2015中新加的特性,它的产生,主要有以下两个原因:一是使得函数表达式(匿名函数)有更简洁的语法,二是它拥有词法作用域的this值,也 ...
随机推荐
- 修改Oracle数据库用户的密码
修改数据库用户system密码的两个方法: 方法一: alter user system identified by password; 方法二: password system;
- HDOJ/HDU 2352 Verdis Quo(罗马数字与10进制数的转换)
Problem Description The Romans used letters from their Latin alphabet to represent each of the seven ...
- hdoj 3785 寻找大富翁【优先队列+sort排序】
寻找大富翁 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- [置顶] cocos2dx sqllite 增删查改等操作
首先导入文件shell.c sqllite3.c sqlite3.h sqlite3etx.h文件(注意在生成安卓项目是 不要将shell.c写进android.mk文件中,写进去在cywin中生成会 ...
- typeof和instanceof 运算符
instanceof运算符与typeof运算符相似,用于识别正在处理的对象的类型,但是在使用 typeof 运算符时采用引用类型存储值会出现一个问题. 无论引用的是什么类型的对象,它都返回 " ...
- hibernate性能消耗太狠了。果断减肥引发的连串意外惊喜
近期在云服务器上新部署了一个项目 硬件配置 CPU: 2核 内存: 4096 MB (I/O优化) 开始是调试测试在用 没发觉,今天我看了下监控 cpu使用率达到了60-70% 而且一直持续 我 ...
- ios 利用Reveal来调试界面2--真机调试(步骤详解)
使用真机调试我们的App界面,如果你的真机是没有越狱的设备,那么使用Reveal来调试UI的步骤是最麻烦的.
- Cocos2d 3.0继承自Sprite的类在addChild后出现故障
当继承自Sprite的类被addChild到其它的Node里后出现例如以下图问题,说明没有调用父类Sprite::init()的方法.由于父类Sprite里的_textureAtlas须要初始化为nu ...
- [CSS] DOM Hierarchy Pseudo Classes :first-child :last-child :nth-child (demystified)
DOM hierarchy pseudo-classes allow you to style specific elements based on where they fall in the hi ...
- [ES7] Exploring ES2016 Decorators
Original artial --> link How descorator looks like: @mydecorator function myFun(){ ... } Descorat ...