weixin_33743248 2016-08-12 18:43 采纳率: 0%
浏览 69

React Ajax和jsx

My array that I filled with my ajax request result will not appears in my react components unordered list.

When I do a console.log of the array I get:

[] 0 : "Career"

1 : "Middle College for Technology Careers"

2 : "Careers New Zealand"

3 : "Careers for Veterans 200"

4 : "Northside Health Careers High School"

5 : "Barbie's careers"

6 : "Edward M. Kennedy Academy for Health Careers"

7 : "The Careers Group, University of London"

8 : "Careers advisor"

9 : "Careers advisory service"

length : 10

proto : Array[0]

When I call this array inside my react component it does not render. Here is my code below.

HTML

<div id="content"></div>

JS

  //array that the titles will be loaded into
  var response = [];

  $.ajax({
    // request type ( GET or POST )
  type: "GET",

    // the URL to which the request is sent
  url: "https://en.wikipedia.org/w/api.php?",

    // data to be sent to the server
  data: { action:'query', format:'json', list: 'search', srsearch: 'careers', srwhat: 'text', srprop: 'timestamp', continue: ''},

    // The type of data that you're expecting back from the server
dataType: 'jsonp',
crossDomain : true,
// Function to be called if the request succeeds
success: function( jsondata ){

for(var i = 0; i < jsondata.query.search.length; i++){
    //console.log( jsondata.query.search[i].title );
  //pushes each result into the array named list
  response.push(jsondata.query.search[i].title);
}

}
});



 console.log(response)


var App = React.createClass({
render: function(){
//var title = list;
return(
  <div>
  <h1>Hello World</h1>
  <p>The world is full of oppurtunity</p>
    <ul>
      <li>{response[0]}</li>
      <li>Freedom</li>
      <li>Lozve</li>
      <li>Money</li>
    </ul>
    </div>
    );
  }
  });

   ReactDOM.render(<App/>, document.getElementById("content"));

Here is a link to my code pen where all my code is, http://codepen.io/vhall_io/pen/vKQrQN/ . I inserted response[0] into the first li tag and it will not render. Please help me render this.

  • 写回答

1条回答 默认 最新

  • weixin_33701294 2016-08-12 18:52
    关注

    You have to update state. It wont render is you are adding it to variable. as it is ajax call so your html render first

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)