经过几天根据官方文档和博园中一些大牛的文章,在了解过基础的语法和组件后,总结一下:

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初探的更多相关文章

  1. React 初探

    React 简单介绍 先说 React 与 React Native 他们是真的亲戚,可不像 Java 和 Javascript 一样. 其实第一次看到 React 的语法我是拒绝的,因为这么丑的写法 ...

  2. react初探(二)之父子组件通信、封装公共组件

    一.前言 在组件方面react和Vue一样的,核心思想玩的就是组件,下面举两个组件常用的情景. 场景一:假如我们现在有一个页面包含表格以及多个弹框,这种时候如果将这个页面的业务代码写在一个组件中,那么 ...

  3. React学习之一:React初探

    一,React简介 React是由Facebook和Instagram开发的一套创建用户界面的JavaScript库.许多人认为React是MVC中的V. React创建的目的是为了:构建数据随时会改 ...

  4. facebook开源前端UI框架React初探

    最近最火的前端UI框架非React莫属了.赶紧找时间了解一下. 项目地址:http://facebook.github.io/react/ 官方的介绍:A JavaScript library for ...

  5. react初探(一)之JSX、状态(state)管理、条件渲染、事件处理

    前言: 最近收到组长通知我们项目组后面新开的项目准备统一技术栈为react,目前我的情况是三大框架只会angular和Vue.在实际项目中只使用过一次angular5,其余项目都是使用Vue写的.写篇 ...

  6. React Native初探

    前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...

  7. 【优质】React的学习资源

    React的学习资源 github 地址: https://github.com/LeuisKen/react-collection https://github.com/reactnativecn/ ...

  8. React Native指南汇集了各类react-native学习资源、开源App和组件

    来自:https://github.com/ele828/react-native-guide React Native指南汇集了各类react-native学习资源.开源App和组件 React-N ...

  9. 简单理解ECMAScript2015中的箭头函数新特性

    箭头函数(Arrow functions),是ECMAScript2015中新加的特性,它的产生,主要有以下两个原因:一是使得函数表达式(匿名函数)有更简洁的语法,二是它拥有词法作用域的this值,也 ...

随机推荐

  1. [转]SVN的trunk branch tag

    Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn://proj/,那么标准的svn布局是 svn://proj/|+-trunk+-branches+-ta ...

  2. Unity给力插件之ShaderForge(一)

    这是一个用来制作shader的插件,也是一个很好的学习shader的工具.这个插件上手很容易,但是要用它来制作理想的Shader,需要下点功夫. 这儿先列举出基础知识,以及我的一些实践.以后我还会继续 ...

  3. Python初始值表示为无穷大

    之前只知道设置变量的初始值为0.今天在写网络路径分析的时候,为了找到离任意坐标距离最近的节点,初始设置最短距离为无穷大,然后不断的去替换,直到找到最近的节点. 刚开始设置是min_dis = 9999 ...

  4. 用 localhost 访问正常,替换成 IP ,部分 CSS 或 JS 就失效了

    这应该是浏览器的兼容性问题. 经测试,只要不是360浏览器的兼容模式,将 localhost 替换成 IP 无影响. 来自为知笔记(Wiz)

  5. java 删除字符串中的特定字符

    /** * Delete any character in a given String. * @param inString the original String * @param charsTo ...

  6. MVC母版面,子页的脚本生成在最后

  7. python学习之字符串

    最近在学习python,随手做些记录,方便以后回顾 #字符串是不可再改变的序列aa='abcd'#aa[2:]='ff' #报错,不可直接赋值#字符串格式化:使用格式化操作符即百分号%来实现print ...

  8. SecureCRT 无法删除字符

    1. 2.

  9. PAT 1070. Mooncake (25)

    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival.  Many types ...

  10. 圣诞节来了,雪花纷飞的CSS3动画,还不首页用起来

    圣诞节来了,冬天来了,怎么可以没有雪花纷飞效果,昨天下班前折腾了一会儿,弄了个雪花纷飞的实例,有兴趣的可以交流分享下. 原文链接:http://www.html5think.com/article/i ...