React开发入门
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello world!</title>
<script src = "../../build/react.js"></script>
<script src = "../../build/react-dom.js"></script>
<script src = "../../build/browser.min.js"></script>
</head>
<body>
<div id = "example"></div>
<script type="text/babel">
ReactDOM.render(
<h1>Hello,World!</h1>,
document.getElementById('example')
);
</script>
</body>
</html>
<script type="text/babel">
ReactDOM.render(
<h1>Hello,World!</h1>,
document.getElementById('example')
);
</script>
ReactDOM.render(
<h1>Hello,World!</h1>,
document.getElementById('example')
);

var animals = ['dog','cat','pig'];
ReactDOM.render(
<div>
{
animals.map(function(animal) {
return <h1>{animal}</h1>
})
}
</div>,
document.getElementById('example')
);
Warning: Each child in an array or iterator should have a unique "key" prop. Check the top-level render call using <div>
var animals = ['dog','cat','pig'];
ReactDOM.render(
<div>
{
animals.map(function(animal,key) {
return <h1 key = {key}>{animal}</h1>
})
}
</div>,
document.getElementById('example')
);

browser.min.js:3 XMLHttpRequest cannot load file:///Users/**/***/React/MyReactDemo/helloworld/src/helloworld.js.
Cross origin requests are only supported for protocol schemes:
http, data, chrome, chrome-extension, https, chrome-extension-resource.
startup chrome with --disable-web-security
On Windows: chrome.exe --disable-web-security On Mac: open /Applications/Google\ Chrome.app/ --args --disable-web-security
http://localhost:63342/MyReactDemo/helloworld/src/helloworld.html
file:///Users/zhanggui/zhanggui/React/MyReactDemo/helloworld/src/helloworld.html
3、组件化
React.createClass方法就是用于生成一个组件类,比如:
var ZGButton = React.createClass({
render:function() {
return <button>ZG{this.props.name}</button>
}
});
ReactDOM.render(
<ZGButton name = 'Button1'/>,
document.getElementById('example')
);

var zGButton = React.createClass({
render:function() {
return <button>ZG{this.props.name}</button>
}
});
ReactDOM.render(
<zGButton name="Button2">Button</zGButton>,
document.getElementById('example')
);
var Students = React.createClass({
render:function() {
return (
<ol>
{
React.Children.map(this.props.children,function(child) {
return <li>{child}</li>
})
}
</ol>
);
}
});
ReactDOM.render(
<Students>
<span>zhangsan</span>
<span>lisi</span>
</Students>,
document.getElementById('example')
);

var Student = React.createClass({
propTypes: {
myName:React.PropTypes.string.isRequired,
},
render:function() {
return <h1>
{this.props.myName}
</h1>
}
});
var myNameStr = "React";
ReactDOM.render(
<Student myName = {myNameStr} />,
document.getElementById('example')
);
var Student = React.createClass({
getDefaultProps: function() {
return {
myName:"Default React"
}
}, propTypes: {
myName:React.PropTypes.string.isRequired,
},
render:function() {
return <h1>
{this.props.myName}
</h1>
}
});
var MyComponment = React.createClass({
render:function(){
return (
<div>
<input type = "text" ref = "myTextInput"/>
<input type = "button" value = "Focus the text input" onClick={this.handleClick}/>
</div>
);
},
handleClick:function() {
// alert(this.refs.myTextInput);
this.refs.myTextInput.focus();
}
});
ReactDOM.render(
<MyComponment/>,
document.getElementById('example')
);
var LinkButton = React.createClass({
getInitialState:function () {
return {linked:false};
},
handleClick:function() {
this.setState({linked:!this.state.linked});
},
render:function() {
var text = this.state.linked? 'linked':'not linked';
return (
<p onClick={this.handleClick}>
You {text} this. Click to toggle
</p>
);
}
});
ReactDOM.render(
<LinkButton/>,
document.getElementById('example')
);
var Form = React.createClass({
getInitialState:function() {
return {value:'Hello'}; },
handleChange:function(event) {
this.setState({value:event.target.value});
},
render:function() {
var value = this.state.value;
return (
<div>
<input type="text" value = {value} onChange={this.handleChange}/>
<p>{value}</p>
</div> );
}
});
ReactDOM.render(
<Form/>,
document.getElementById('example')
);
var MyButton = React.createClass({ componentDidMount:function() {
alert("已经装载");
},
componentWillMount:function() {
alert("将要装载");
},
componentWillUpdate:function() {
alert("将要更新");
},
componentDidUpdate:function() {
alert("已经更新");
},
componentWillUnmount:function() {
alert("将要移除");
},
render:function(){
return (
<button>MyButton</button>
);
},
});
var LoadButton = React.createClass({
loadMyButton:function() {
ReactDOM.render(
<MyButton/>,
document.getElementById('myBTN')
);
},
removeMyButton:function() {
var result = ReactDOM.unmountComponentAtNode(document.getElementById('myBTN'));
console.log(result);
},
render:function() {
return (
<div>
<button onClick={this.removeMyButton}>卸载MyButton</button>
<button onClick={this.loadMyButton}>装载MyButton</button>
<div id="myBTN">这里是mybuttonquyu</div>
</div> );
}
});
ReactDOM.render(
<LoadButton/>,
document.getElementById('example')
);
var UserGist = React.createClass({
getInitialState:function() {
return {
username:'',
lastGistUrl:''
}
},
componentDidMount:function(){
$.get(this.props.source,function(result){
var lastGist = result[0];
if (this.isMounted()) {
this.setState({
username:lastGist.owner.login,
lastGistUrl:lastGist.html_url
}
);
}
}.bind(this));
},
render:function() {
return (
<div> {this.state.username}'s last gist is
<a href={this.state.lastGistUrl}>here</a> </div>
);
}
});
ReactDOM.render(
<UserGist source = "https://api.github.com/users/octocat/gists"/>,
document.getElementById('example')
);
React开发入门的更多相关文章
- 基于Nodejs生态圈的TypeScript+React开发入门教程
基于Nodejs生态圈的TypeScript+React开发入门教程 概述 本教程旨在为基于Nodejs npm生态圈的前端程序开发提供入门讲解. Nodejs是什么 Nodejs是一个高性能Ja ...
- React开发入门:以开发Todo List为例
目录 概述 React基本概念 JSX是什么? 设置React APP 初始化APP 应用结构 探索第一个React组件 index.js 变量和props JSX中的变量 组件props props ...
- 前端React开发入门笔记
什么是React React是一个JavaScript库,是由FaceBook和Instagram开发的,主要用于用户创建图形化界面. Hello world <!DOCTYPE html> ...
- React组件开发入门
React 组件开发入门 Introduction 本文组成: Ryan Clark文章Getting started with React的翻译. 博主的实践心得. React由Facebook的程 ...
- React Native入门教程 1 -- 开发环境搭建
有人问我为啥很久不更新博客..我只能说在学校宿舍真的没有学习的环境..基本上在宿舍里面很颓废..不过要毕业找工作了,我要渐渐把这个心态调整过来,就从react-native第一篇博客开始.话说RN也出 ...
- React.js 入门与实战之开发适配PC端及移动端新闻头条平台课程上线了
原文发表于我的技术博客 我在慕课网的「React.js 入门与实战之开发适配PC端及移动端新闻头条平台」课程已经上线了,文章中是目前整个课程的大纲,以后此课程还会保持持续更新,此大纲文档也会保持更新, ...
- React Native入门——布局实践:开发京东client首页(一)
有了一些对React Native开发的简单了解,让我们从实战出发.一起来构建一个简单的京东client. 这个client是仿照之前版本号的京东client开发的Android版应用,来源于CSDN ...
- 二、react开发环境配置与webpack入门
Webpack 模块打包工具(module bundler)功能: 将 CSS.图片与其他资源打包 打包之前预处理(Less.CoffeeScript.JSX.ES6 等)档案 依 entry 文件不 ...
- React.js入门笔记
# React.js入门笔记 核心提示 这是本人学习react.js的第一篇入门笔记,估计也会是该系列涵盖内容最多的笔记,主要内容来自英文官方文档的快速上手部分和阮一峰博客教程.当然,还有我自己尝试的 ...
随机推荐
- 烂泥:智能DNS使用与配置
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 公司的业务现在已经扩展到海外,对外提供的统一接口都是通过域名来解析的,但是海外用户访问国 ...
- openstack-swift云存储部署(一)
最近因为工作的需要搭建了一套swift云存储架构 我们先来解读一下里面的技术知识点:swift服务是属于openstack中的一种组件服务,openstack中的组件服务还有keystone.Nova ...
- Linux常用的命令以及一些常见的问题
常用的linux 命令 1查看Linux版本 1.1 查看linux的内核版本 uname -r 或者uname -a 1.2 查看linux的具体版本号 cat /proc/version 1. ...
- 深入理解java内存模型系列文章
转载关于java内存模型的系列文章,写的非常好. 深入理解java内存模型(一)--基础 深入理解java内存模型(二)--重排序 深入理解java内存模型(三)--顺序一致性 深入理解java内存模 ...
- linux basic commands
1. man - an interface to the on-line reference manuals $man man 2. apt - advanced package tool SEE A ...
- ASP.NET 5 DNX SDK删除旧版本
ASP.NET 5各种升级后旧版本的DNX不会删除,想删除旧版本的DNX,可以通过以下命令完成 首先打开CMD或者Powershell 1.先输入dnvm看看命令中是否有uninstall 2.如果没 ...
- Codeforces Round #378 (Div. 2)
A: 思路: 水题,没啥意思; B: 思路: 暴力,也没啥意思; C: 思路: 思维,可以发现从前往后和为b[i]的分成一块,然后这一块里面如果都相同就没法开始吃,然后再暴力找到那个最大的且能一开始就 ...
- Ajax与json
Ajax Ajax简介 Ajax技术,从用户发送请求到获取响应,当用户界面在整个过程中不会受到干扰,而且我们可以在必要的时候只刷新页面的一小部分,而不用刷新整个页面,即"无刷新"技 ...
- WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
http://dl.bintray.com/sequenceiq/sequenceiq-bin/ http://www.secdoctor.com/html/yyjs/31101.html
- bootstrap 组件
bootstrap 组件 1下拉菜单(dropdown) 下拉菜单切换(dropdown-toggle) 下拉菜单对齐(dropdown-menu-right-右对齐) 下拉菜单分割线(di ...