Uncaught TypeError: Cannot read property ‘props’ of undefined
来源:互联网 发布:聊天软件哪个最好 编辑:程序博客网 时间:2024/05/22 01:57
Uncaught TypeError: Cannot read property ‘props’ of undefined
错误行如下:
源码
var ProductCategoryRow = React.createClass({ render: function() { return (<tr><th colSpan='2'>{this.props.category}</th></tr>); }});var ProductRow = React.createClass({ render: function() { var name = this.props.product.stocked ? this.props.product.name : <span style={{color: 'red'}}> {this.props.product.name} </span>; return ( <tr> <td>{name}</td> <td>{this.props.product.price}</td> </tr> ); } });var ProductTable = React.createClass({ render: function() { var rows = []; var lastCategory = null; this.props.products.forEach(function(product){ console.log("this:",this); if (product.name.indexOf(this.props.filterText) === -1 || (!product.stocked && this.props.inStockOnly)) { return; } if (product.category !== lastCategory) { rows.push(<ProductCategoryRow category={product.category} key={product.category} />); } rows.push(<ProductRow product={product} key={product.name} />); lastCategory = product.category; }); return ( <table> <thead> <tr> <th>Name</th> <th>Price</th> </tr> </thead> <tbody>{rows}</tbody> </table> ); }});var SearchBar = React.createClass({ render: function() { return ( <form> <input type="text" placeholder="Search..." /> <p> <input type="checkbox" /> {' '} Only show products in stock </p> </form> ); }});var FilterableProductTable = React.createClass({ getInitialState: function() { return { filterText: 'ball', inStockOnly: false }; }, render: function() { return ( <div> <SearchBar filterText={this.state.filterText} inStockOnly={this.state.inStockOnly} /> <ProductTable products={this.props.products} filterText={this.state.filterText} inStockOnly={this.state.inStockOnly} /> </div> ); }});var PRODUCTS = [ {category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Football'}, {category: 'Sporting Goods', price: '$9.99', stocked: true, name: 'Baseball'}, {category: 'Sporting Goods', price: '$29.99', stocked: false, name: 'Basketball'}, {category: 'Electronics', price: '$99.99', stocked: true, name: 'iPod Touch'}, {category: 'Electronics', price: '$399.99', stocked: false, name: 'iPhone 5'}, {category: 'Electronics', price: '$199.99', stocked: true, name: 'Nexus 7'}];ReactDOM.render( <FilterableProductTable products={PRODUCTS} />, document.getElementById('example'));
解决方法:1、var that=this;
var ProductTable = React.createClass({ render: function() { var rows = []; var lastCategory = null; var that=this; this.props.products.forEach(function(product){ console.log("this:",this); if (product.name.indexOf(that.props.filterText) === -1 || (!product.stocked && that.props.inStockOnly)) { return; } if (product.category !== lastCategory) { rows.push(<ProductCategoryRow category={product.category} key={product.category} />); } rows.push(<ProductRow product={product} key={product.name} />); lastCategory = product.category; }); return ( <table> <thead> <tr> <th>Name</th> <th>Price</th> </tr> </thead> <tbody>{rows}</tbody> </table> ); }});
方法2:=>箭头函数
var ProductTable = React.createClass({ render: function() { var rows = []; var lastCategory = null; this.props.products.forEach((product)=>{ console.log("this:",this); if (product.name.indexOf(this.props.filterText) === -1 || (!product.stocked && this.props.inStockOnly)) { return; } if (product.category !== lastCategory) { rows.push(<ProductCategoryRow category={product.category} key={product.category} />); } rows.push(<ProductRow product={product} key={product.name} />); lastCategory = product.category; }); return ( <table> <thead> <tr> <th>Name</th> <th>Price</th> </tr> </thead> <tbody>{rows}</tbody> </table> ); }});
方法3:map遍历(此方法没有亲自测试)
阅读全文
0 0
- Uncaught TypeError: Cannot read property ‘props’ of undefined
- Extjs4---Uncaught TypeError: Cannot read property ‘items’ of undefined
- 【StackFlow】Uncaught TypeError: Cannot read property 'pageCount' of undefined
- Ckeditor: Uncaught TypeError: Cannot read property 'keyCode' of undefined 解决方法
- Uncaught TypeError: Cannot read property 'msie' of undefined问题解决方案
- Uncaught TypeError: Cannot read property 'options' of undefined 错误
- Uncaught TypeError: Cannot read property 'length' of undefined
- 错误:Uncaught TypeError: Cannot read property 'WebSocket' of undefined
- Uncaught TypeError: Cannot read property 'value' of undefined
- Uncaught TypeError: Cannot read property 'plugin' of undefined
- react Uncaught TypeError: Cannot read property 'title' of undefined
- Uncaught TypeError: Cannot read property 'width' of undefined
- Uncaught TypeError: Cannot read property 'substring' of undefined
- Uncaught TypeError: Cannot read property 'PRINT_INIT' of undefined user:100
- Uncaught TypeError: Cannot read property 'substring' of undefined
- extjs4:Uncaught TypeError: Cannot read property 'processed' of undefined
- Datatables 之Uncaught TypeError: Cannot read property 'mData' of undefined
- background.js Uncaught TypeError: Cannot read property 'xxx' of undefined
- Data Structures and algorithm analysis—1.2.1&1.2.2&1.2.3 exponents&logarithms&series(数据结构—指数,对数,级数)
- Flex连接数据库三种步骤
- 使用微信的 JS SDK 选取手机照片并进行上传,Iphone无法显示缩略图
- TensorFlow学习(十):图像预处理
- grails Gorm查询错误
- Uncaught TypeError: Cannot read property ‘props’ of undefined
- Oracle存储过程介绍
- 领域驱动设计系列(1)通过现实例子显示领域驱动设计的威力
- 『ORACLE』创建物化视图(11g)
- 自定义 ViewGroup 实现子 View 层叠效果
- MySQL远程访问及访问速度慢等问题解决
- https
- 在JS中获取后台的传到该页面的值
- ImageLoader的实现(1)-LruCache和DiakLruCache