webpack学习(六)—webpack+react+es6(第3篇)
接上篇 : webpack学习(六)—webpack+react+es6(第2篇)
上篇其实是有问题的,问题在取服务器数据这块。this.props 表示那些一旦定义,就不再改变的特性,而 this.state 是会随着用户互动而产生变化的特性【1】。服务器数据是变化的,应该作为state而不是props。
参考了其他的博客后,改写一遍。例子:
src/js/app.js
import React from 'react';
import ReactDOM from 'react-dom';
import ImgDemo from './imgDemo'; ReactDOM.render(
<ImgDemo/>,
document.getElementById('imgDemo')
)
src/js/imgDemo.js【2】
import React from 'react';
import OneImg from './oneImg'
class ImgDemo extends React.Component{
constructor(props) {
super(props);
//我们使用state里面的datalist,来保存所有服务端传来的数据
//刚开始的时候,内容为空
this.state = {
datalist:[]
}
} render() {
//当react库运行到render方法的时候,就会遍历所有state中datalist的项目,第一遍为空
return <div>
{
this.state.datalist.map((arr,index)=>
<OneImg oneData={arr} key={index}/>
)
}
</div>;
} //可以看到,我们的类被构造的时候本身自带的state中services是个空数组,我们需要用内容填充他
//查看react的文档的《组件生命周期》这一页(这一页很重要,一定要明白各个函数在什么情况下会被触发),发现它建议我们在每个组件显示完毕,之后使用componentDidMount函数来获取需要的数据,那就照做
componentDidMount() {
let xhr = new XMLHttpRequest()
//写了个data.json文件模拟服务器,请求这个文件
xhr.open("GET", "http://localhost:8080/src/js/data.json", true) xhr.send() xhr.onreadystatechange = () =>{
if(xhr.readyState === 4 && xhr.status === 200){
let data = JSON.parse(xhr.responseText)
//获得服务器的数据并转换为json,使用setState方法覆盖当前元素的datalist数据
this.setState({
datalist : data
})
}
}
}
} module.exports = ImgDemo
src/js/oneImg.js
import React from 'react';
class OneImg extends React.Component{
render(){
var oneData = this.props.oneData;
return <div>
<img className='img' src={oneData.img}/>
<p>{oneData.word}</p>
</div>
} }
module.exports = OneImg
src/js/data.json(模拟服务器数据用的)
[
{
"img":"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top_ca79a146.png",
"word":"百度的图标"
},
{
"img":"https://www.sogou.com/web/img/logo128_50x2.png",
"word":"搜狗的图标"
}
]
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.img{width:300px;height:100px;}
</style>
</head>
<body>
<div id="imgDemo"></div>
<script src="build/bundle.js"></script>
</body>
</html>
其中,package.json、webpack.config.js跟上篇一致。这里不再列出。
安装: npm init
启动:npm run dev
项目地址:http://localhost:8080/index.html
---------------------------
至此,OK
注意:
1)网上的例子很多ajax请求用了jquery。其实,react是不依赖任何其他库的。
2)网上的戏子大多将ajax的url作为prop传给组件,ajax请求时通过this.props对象获取。本例是直接写在请求里面。这个稍作修改就一致了。
参考:
【1】http://www.ruanyifeng.com/blog/2015/03/react.html
【2】React如何从后端获取数据并渲染到前端?中的cheoyx的答案
webpack学习(六)—webpack+react+es6(第3篇)的更多相关文章
- webpack学习笔记—webpack安装、基本配置
文章结构: 什么是webpack? 安装webpack 'webpack基本配置 一.什么是webpack? 在学习react时发现大部分文章都是react和webpack结合使用的,所以在学reac ...
- webpack 学习资料
webpack 学习资料 webpack 学习资料 网址 webpack 中文版 https://webpack.docschina.org/configuration/dev-server/
- webpack学习(六)—webpack+react+es6(第2篇)
接上篇 webpack学习(五)—webpack+react+es6(第1篇) 本文做个简单的图片加文字的页面.其中,配置文件跟上篇一致.项目结构: index.html <!DO ...
- reactjs学习一(环境搭配react+es6+webpack热部署)
reactjs学习一(环境搭配react+es6+webpack热部署) 本文的源码在这里下载 https://github.com/tianxiangbing/webpack-study 或者使 ...
- webpack学习(五)—webpack+react+es6(第1篇)
如果你看过webpack学习系列的前一个文章,接下来做的东西会比较简单 :webpack学习(四)— webpack-dev-server react发展的很快,现在大部分开发react相关的项目,都 ...
- webpack+react+es6开发模式
一.前言 实习了两个月,把在公司用到的前端开发模式做个简单的整理.公司里前端开发模式webpack+react+redux+es6,这里去掉了redux. webpack, react, redux等 ...
- Webpack+React+ES6 最新环境搭建和配置(2017年)
刚刚学习React,发现React在ES6下的语法才是本体,结合ES6新的语言特性,使组件化开发显得更加直观.而且现在的Angular2也开始使用支持强类型的TypeScript,转译(transpi ...
- Webpack+React+ES6开发模式入门指南
React无疑是今年最火的前端框架,github上的star直逼30,000,基于React的React Native的star也直逼20,000.有了React,组件化似乎不再步履蹒跚,有了Reac ...
- [webpack] 配置react+es6开发环境
写在前面 每次开新项目都要重新安装需要的包,简单记录一下. 以下仅包含最简单的功能: 编译react 编译es6 打包src中入口文件index.js至dist webpack配置react+es6开 ...
随机推荐
- hdu_1863_畅通工程_201403122000
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- [Cypress] Interact with Hidden Elements in a Cypress Test
We often only show UI elements as a result of some user interaction. Cypress detects visibility and ...
- 2017全面JAVA面试经历总结
https://wenku.baidu.com/view/05e8f71afbd6195f312b3169a45177232f60e474.html?from=search JAVA常见面试题及解答2 ...
- JS容易犯错的this和作用域
var someuser = { name: 'byvoid', func: function() { console.log(this.name); } }; var foo = { name: ' ...
- codeforces 931E Logical Expression dp
time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...
- Ionic学习记录(一):ionic及cordova安装、创建第一个应用、项目结构
目录: 一.ionic的安装 二.创建第一个应用程序 三.浏览器中预览应用 四.项目结构 五.添加页面 一.ionic的安装 使用Ionic创建和开发应用程序主要通过Ionic命令行实用程序(“CLI ...
- B - Helpful Maths
Problem description Xenia the beginner mathematician is a third year student at elementary school. S ...
- 基于NPOI的扩展
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.HSS ...
- Hibernate多表映射(三)
一对多|多对一 一个分类对应多个商品,一个商品只属于一个分类 创建分类表 products用set装,set特点值不能够重复 package com.hibernate.domain; import ...
- SQLServer2008 将“单个用户”改为“多用户”
一开始是要想要分离掉数据库,然后将其删除 不知道为什么一直分离不了,试了很多次,又尝试直接删除 结果数据库突然显示成了“单个用户” 尝试查看其属性,或者“新建查询”也都报错,提示已经有其他用户建立了连 ...