简体   繁体   English

无法重新渲染子组件reactjs

[英]can't re-render child component reactjs

Good day. 美好的一天。 I can't rerender my child component when state changed. 状态更改后,我无法重新渲染子组件。

export default class Featured extends React.Component {

constructor(){
  super()
  this.state = {
    result: []
  }
}

componentDidMount(){
  $.ajax({
    type: "GET",
    dataType:'JSONP',
    beforeSend: function (request){
      request.setRequestHeader("Authorization", Config.key);
    },
    url: "myUrl",
    success: function(data) {
      this.setState({
        result: data.users
      });
      console.log(this.state.result);//here i got my Array
    }.bind(this)
  });
}

componentWillUnmount(){
  this.serverRequest.abort();
}

render() {
  return (
    <div class="">
      <div class="row">
        <Card data={this.state.result} />
      </div>
    </div>
  );
 }
}

I changed state when ajax request success and i have no idea how to rerender my component . 当ajax请求成功时,我更改了状态,但我不知道如何重新渲染组件。 Here is some code of 这是一些代码

export default class extends React.Component {

render() {
  console.log(this.props.data);
  return (
    <div class="flex-container">
      { 
        this.props.data.map(function(item, index) {
          return <div class="col-xs-12 col-sm-6 col-md-12" key={item._id}>

What did i miss? 我错过了什么? Here is console screen http://prntscr.com/ap24x5 这是控制台屏幕http://prntscr.com/ap24x5

Your syntax is incorrect couple of things I can spot right away: 您的语法不正确,但我很快就会发现:

  • you cant use class in the render function, you must use className 您不能在render函数中使用class,而必须使用className

  • your second component has incorrect incorrect syntax, you need to call it something: 您的第二个组件的语法不正确,您需要将其命名为:

    export default class MyComponent extends React.Component {

Since JSX is JavaScript, identifiers such as class and for are discouraged as XML attribute names. 由于JSX是JavaScript,因此不建议将诸如class和for之类的标识符用作XML属性名称。 Instead, React DOM components expect DOM property names like className and htmlFor, respectively. 相反,React DOM组件期望DOM属性名称分别为className和htmlFor。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM