扫码关注官方订阅号
通过ajax获取的一个对象传给state的data 先转成string 再转成对象,可是转的时候报错。如果在ajax里直接只传一个字符串给data就可以
光阴似箭催人老,日月如移越少年。
看你这段代码是用的 ES2015 语法,可以直接用箭头函数来回避 this 的问题,如:
this
var Home = React.createClass({ // ...... getData() { $.ajax({ // .... success: data => { // .... this.setState(/* ... */); } }); } });
但是 ES6 的语法现在并不是所有浏览器都支持得很好,如果使用 ES5 的语法,可以申明一个 _this 变量暂存 this,如
_this
var Home = React.createClass({ // ...... getData: function() { var _this = this; $.ajax({ // .... success: function(data) { // .... _this.setState(/* ... */); } }); } });
要么就在外部保存this,如
var _this = this;
要么就用箭头符号,如
success: () => { // ... }
Home:getdata() success里 this.setState(data)render : <Kem data={this.state}>getInitialState: return {}
Kem
var data =this.props.data
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
看你这段代码是用的 ES2015 语法,可以直接用箭头函数来回避
this的问题,如:但是 ES6 的语法现在并不是所有浏览器都支持得很好,如果使用 ES5 的语法,可以申明一个
_this变量暂存this,如要么就在外部保存this,如
要么就用箭头符号,如
Home:
getdata() success里 this.setState(data)
render : <Kem data={this.state}>
getInitialState: return {}
Kem
var data =this.props.data