2017-08-09 48 views
0

我使用axios.get的回調來設置React組件的狀態。響應的data屬性包含一個用於設置狀態的對象數組。安裝組件和setState後返回undefined的對象屬性

當我將狀態屬性記錄到控制檯時,沒有問題,我可以看到對象數組。但是,如果我嘗試登錄這些對象的一個​​單獨我得到的錯誤:

不能ResponsiveTable.render

看空的財產「0」

下面是我的組件代碼:

class ResponsiveTable extends React.Component { 

    constructor (props) { 
     super(props) 
     this.state = { 
      returnedQuery: null 
     }; 
    } 

    componentDidMount() { 
     axios.get('/api/latestLeads') 
      .then((response) => { 
       this.setState({ 
        returnedQuery: response.data 
       }); 
      }) 
      .catch(function (error) { 
       console.log(error); 
      }); 
    } 

    render() { 

     console.log(this.state.returnedQuery[0]); 

     return (
      <div> 
       <h1>test</h1> 
      </div> 
     ); 
    } 
} 
+0

'render'是'componentDidMount' – azium

回答

1

當然,首先渲染你的returnedQuery爲空,所以你得到這個錯誤。

如果你想使用this.state.returnedQuery[0]檢查,如果它的存在,它的長度> 0:

render() { 
     if (this.state.returnedQuery && this.state.returnedQuery.lenth > 0){ 
       return (
        <div> 
         {this.state.returnedQuery.map(...)} 
        </div> 
       ); 
     } else { 
      return <div>loading data...</div> 
     } 
    } 
+0

之前叫我應該解釋的情況好一點的計劃是使用.MAP方法通過返回數組迭代渲染行內填充表格單元格。所以我需要數組屬性中的對象在渲染函數 –

+0

@NiallFaucher中可用,請參閱,起初渲染將從構造函數中''this.state.returnedQuery === null'工作'else'。然後,在axios查詢'setState'後第二次更新,但'this.state.returnedQuery === [{},{},{}]'並且將工作if語句的第一部分。 – Andrew

0

你可以試試這個:

class ResponsiveTable extends React.Component { 

    constructor (props) { 
     super(props) 
     this.state = { 
      returnedQuery: null 
     }; 
     this.getData(); 
    } 

    getData =() => { 
     axios.get('/api/latestLeads') 
      .then((response) => { 
       this.setState({ 
        returnedQuery: response.data 
       },() => {console.log(this.state.returnedQuery);}); //What does this console.log() say? 
      }) 
      .catch(function (error) { 
       console.log(error); 
      }); 
    } 

    render() { 

     console.log(this.state.returnedQuery[0]); 

     return (
      <div> 
       <h1>test</h1> 
      </div> 
     ); 
    } 
} 
+0

迴應你的問題console.log();返回數組,但使用console.log(this.state.returnedQuery [0]);在渲染功能仍然會導致一個錯誤,我認爲這是由安德魯點解釋第一次渲染。我需要找到一種先渲染的方法,直到response.data準備好爲止 –

+0

但是爲什麼?你改變狀態的那一刻,反應重新呈現。你想用'returnedQuery [0]'做什麼?我不會反對,並嘗試強制'render()'等待或不行。像這樣 – Nocebo

0

我發現這是非常相似安德魯的上述解決方案,我使用組件的內部狀態來確定axios.get方法是否已經返回。下面是工作代碼,我現在可以訪問返回數組中的元素及其屬性。

class ResponsiveTable extends React.Component { 

    constructor (props) { 
     super(props) 
     this.state = { 
      returnedQuery: null 
     }; 
    } 

    componentDidMount() { 

     const self = this; 

     // let returnedQuery; 
     axios.get('/api/latestLeads') 
      .then((response) => { 
       self.setState({ 
        returnedQuery: response.data 
       }); 
       console.log(response.data); 
      }) 
      .catch(function (error) { 
       console.log(error); 
      }); 
    } 

    render() { 
     return (
      <div> 
       <h1>{'This will always render'}</h1> 
       { this.state && this.state.returnedQuery && 
       <div>{this.state.returnedQuery[0].email}</div> 
       } 
      </div> 
     ) 
    } 
} 
+0

你不需要這樣做:'const self = this;'這是1997年的代碼。而是使用箭頭函數將函數綁定到您的組件: 'myFunction =()=> {...}'您現在可以在正確範圍內的組件中訪問「this」。類似'componentDidiMount'的反應函數通常已經綁定了。嘗試這樣做: 'this.setState(...)' – Nocebo