javascript - react.js 箭头函数返回注意
问题描述
class Greeting extends React.Component {
state={
username:'u1',
user:['u1','u2']
}
constructor(props){
super(props);
this.uList=this.uList.bind(this);
}
uList = () =>{
return this.state.user.map((u)=>{
console.log(u);
return <li>{u}</li>;
});
}
render() {
return (<div>
<p>Hello world</p>
{this.uList}
</div>
);
}
}
完整代码:https ://pastebin.com/raw/Azi65Vnu
我第一次尝试使用 cdn 的 react.js,它只给了我 hello world,但没有渲染列表。
这是为什么?
解决方案
你需要调用函数
render() {
return (<div>
<p>Hello world</p>
{this.uList()}
</div>
);
}
推荐阅读
- emacs - 如何在 Emacs 中突出显示我自己的语法?
- ios - 应用程序图标不填充 xcode 中的整个区域是否很常见?(包括图片)
- node.js - node-cron 代码放置的最佳实践是什么?
- facebook-graph-api - 使用图形 api 从 facebook 提取数据
- ios - 如何在 Swift 3 和 4 中发布 Google Sheets API V4 的值?
- user-interface - 如何摆脱已编译的 Haskell GUI 应用程序的黑色控制台窗口
- django - 在本地测试 Anymail webhook,无法接收信号
- javascript - 电子错误 | 节点.js
- java - 在构造函数中将对象作为参数传递
- json - Moshi 相当于 Gson 的“serializeNulls”