当我渲染这段代码时,comment
数组中的comments
没有显示给组件,这是反应代码
var Board = React.createClass({
getInitialState: function() {
return{comments: ['i am rohith','some call me drohi','some call me rogi']}
}
render: function() {
return(
<div className="board">
{
this.state.comments.map(function(text,i){
return(<Comment key={i}>{text}</Comment>);
})
}
</div>
);
}
});
当我渲染这段代码时,comments数组中的字符串没有显示给组件
答案 0 :(得分:0)
I see the state
is missing out there and cannot be accessed in render function of your code snippet.
this.state.comments
I made some changes, see if it helps.
You can tinker around the snippet. https://codesandbox.io/s/6RxBYqpBQ
import React from 'react';
export default class Board extends React.Component {
constructor(props) {
super(props);
this.state = {
comments: ['i am rohith', 'some call me drohi', 'some call me rogi'],
};
}
render() {
return (
<div className="board">
{this.state.comments.map(function(text, i) {
return <span key={i}>{text} <br /></span>;
})}
</div>
);
}
}