import React, { Component } from 'react';
import PropTypes from 'prop-types';
import './First.css';
import $ from 'jquery';
class First extends Component {
constructor(props) {
super(props);
// create a ref to store the textInput DOM element
//每个事件必须绑定后才可以使用
this.myTextInput = React.createRef();
// this.focusFunc = this.focusFunc.bind(this);
// this.getInput = this.getInput.bind(this);
//定义初始状态
this.state = {
message: false,
inputValue: "inputValue...",
divStyle: {
color: 'red',
backgroundColor: 'green'
},
opacity:0.2,
//fetch
usernameF: '',
lastUrlF: '',
//ajax数据
username: '',
lastUrl: ''
}
}
render() {
let hellotext = this.state.message ? 'like' : 'have\'t liked';
return (
<div>
<h1> {this.props.title} </h1>
<hr />
<input type="text" ref="myTextInput" />
<input type="button" value="Focus the textinput!" onClick={this.focusFunc.bind(this)}/>
<p>{ hellotext }</p>
<hr />
<input type="button" onClick={this.getInput.bind(this)} value="互动" />
<p>{ this.state.inputValue }</p>
<hr />
<div style={this.state.divStyle}>this is div!</div>
<div style={{opacity: this.state.opacity}}>this is div2!</div>
<hr/>
<div>
{this.state.usernameF},
{this.state.lastUrlF}
</div>
<hr />
<div>
{this.state.username},
{this.state.lastUrl}
</div>
</div>
);
}
focusFunc() {
this.refs.myTextInput.focus();//获取真实的DOM节点需要使用refs.name
//重新修改状态值,每次修改以后,自动调用 this.render 方法,再次渲染组件。
this.setState({
message: !this.state.message
});
}
getInput(event) {
// alert();
this.setState({
inputValue: event.target.value
});
}
//生命周期
// Mounting:已插入真实 DOM
// Updating:正在被重新渲染
// Unmounting:已移出真实 DOM
componentWillMount() {
console.log('componentWillMount');
fetch(this.props.source)
.then(res => res.json())
.then(
(result) => {
var lastGist = result[1];
this.setState({
usernameF: lastGist.owner.login,
lastUrlF: lastGist.html_url
});
},
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
(error) => {
this.setState({
// isLoaded: true,
// error
});
}
)
}
componentDidMount() {
let self = this;
console.log('componentDidMount');
$.get(this.props.source,function(res){
var lastGist = res[0];
self.setState({
username: lastGist.owner.login,
lastUrl: lastGist.html_url
});
});
}
componentWillUpdate(prevProps, prevState, snapshot) {
// If we have a snapshot value, we've just added new items.
// Adjust scroll so these new items don't push the old ones out of view.
if (snapshot !== null) {
console.log('componentWillUpdate');
}
}
componentDidUpdate(prevProps, prevState, snapshot) {
// If we have a snapshot value, we've just added new items.
// Adjust scroll so these new items don't push the old ones out of view.
if (snapshot !== null) {
console.log('componentDidUpdate');
}
}
componentWillUnmount() {
console.log('componentWillUnmount');
}
}
//设置props的数据类型
First.proTypes = {
title: PropTypes.func,
}
//设置默认props的title值
First.defaultProps = {
title: 'First'
}
export default First;

react的基本使用,及常用填坑的更多相关文章

  1. React Native工作小技巧及填坑记录

    以下是本人在React Native开发工作中使用的一些小技巧,记录一下. 1.从网络上拉取下来的React Native缺少React和React Native库. 终端 1. cd 项目根目录 2 ...

  2. css 填坑常用代码分享

    以下是常用的代码收集,没有任何技术含量,只是填坑的积累.转载请注明出处,谢谢. 因为提交比较麻烦,后来转置github:https://github.com/jsfront/src/blob/mast ...

  3. React Native填坑之旅--Stateless组件

    Stateless component也叫无状态组件.有三种方法可以创建无状态组件. 坑 一般一个组件是怎么定义的: 很久以前的方法: const Heading = createClass({ re ...

  4. React Native填坑之旅--与Native通信之iOS篇

    终于开始新一篇的填坑之旅了.RN厉害的一个地方就是RN可以和Native组件通信.这个Native组件包括native的库和自定义视图,我们今天主要设计的内容是native库方面的只是.自定义视图的使 ...

  5. React Native填坑之旅--Flow篇(番外)

    flow不是React Native必会的技能,但是作为正式的产品开发优势很有必要掌握的技能之一.所以,算是RN填坑之旅系列的番外篇. Flow是一个静态的检查类型检查工具,设计之初的目的就是为了可以 ...

  6. React Native填坑之旅--重新认识RN

    如同黑夜里的一道光一样,就这么知道了F8. F8是每年一次Facebook每年一次的开发者大会.每次大会都会release相应的APP,iOS.Android都有.之前都是用Native开发的,但是2 ...

  7. React Native填坑之旅--布局篇

    代码在这里: https://github.com/future-challenger/petshop/tree/master/client/petshop/src/controller 回头看看RN ...

  8. React Native填坑之旅--Navigation篇

    React Native的导航有两种,一种是iOS和Android通用的叫做Navigator,一种是支持iOS的叫做NavigatorIOS.我们这里只讨论通用的Navigator.会了Naviga ...

  9. React Native填坑之旅--ListView篇

    列表显示数据,基本什么应用都是必须.今天就来从浅到深的看看React Native的ListView怎么使用.笔者写作的时候RN版本是0.34. 最简单的 //@flow import React f ...

随机推荐

  1. 微信小程序之上传下载交互api

    wx.request(OBJECT) OBJECT参数说明: 参数名 类型 必填 说明 url String 是 开发者服务器接口地址 data Object.String 否 请求的参数 heade ...

  2. hdu5904 LCIS

    这题惩罚我这种经常不管常数的懒人 直接 1e6 TLE 如果1e5对数组枚举过 诶其实很想吐槽些伤心事,但是还是不想在博客上吐口水 不管今年比赛结果如何 请享受比赛 #include<bits/ ...

  3. studio设置File Templates

    从项目的整体风格考虑,对所有类要进行必要的说明,就注释说明来说首先需要说明是作者,文件创建时间,业务功能说明,这几项是基本的内容,而添加这些说明内容以前可能手动的添加文件标题头,这种做法现在都非常过时 ...

  4. js中的0就是false,非0就是true及案例

    在处理js代码判断真假时经常会这么写. 但fun()可能得到的是数字0,这可不是表示的没有值,但是!js中的数字0就是false,非0就是true. 于是0就被无情的当做false了. 已经被这个坑过 ...

  5. Luogu P1860 新魔法药水

    题目大意 具体题面及输入格式戳我! 商店里有\(N\)种药水,每种药水都有一个售价和回收价. 小\(S\) 攒了\(V\)元钱,还会\(M\)种魔法,可以把一些药水合成另一种药水. 他在第一天可以购买 ...

  6. 【BZOJ1018】堵塞的交通(线段树)

    [BZOJ1018]堵塞的交通(线段树) 题面 Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可 以被看成是一个2行C列的矩形网 ...

  7. c#多线程同步之EventWaitHandle使用

    有这么一个场景,我需要借助windows剪贴板把数据插入到word域中. 实现步骤: 1.把剪贴板数据保存到变量. 2.使用剪贴板实现我们的业务. 3.把变量里的数据存回剪贴板. 但是结果却令人诧异, ...

  8. 题目1032:ZOJ

    题目描述: 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出. 输入: 题目包含多组用例,每组用例占一行,包含ZOJ三个 ...

  9. Hadoop的Archive归档命令使用指南

    hadoop不适合小文件的存储,小文件本省就占用了很多的metadata,就会造成namenode越来越大.Hadoop Archives的出现视为了缓解大量小文件消耗namenode内存的问题. 采 ...

  10. MySQL Connector 卸载

    MySQL Connector 安装的时候有时候会遇到很多问题,有时候会卸载失败,导致无法重新安装.测试了网上各种办法,删文件,删注册表,重启,360强行删除都不是很有效.最后发现msizap比较有效 ...