地图内的内容无法呈现

时间:2017-06-20 09:50:45

标签: reactjs

当我渲染这段代码时,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数组中的字符串没有显示给组件

1 个答案:

答案 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>
    );
  }

}