2017-01-13 8 views
0

私はreactjsに新しく、サーバーからデータを受信して​​いますが、状態を更新できません。私は私がJSONデータとしてサーバからの入力を取得しています。.then(応答=> {this.setState({REPONSE})からエラーを受け取る。reactjsを使用してサーバーからデータを受信

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import axios from 'axios'; 

class App extends React.Component { 
    constructor(props) { 
    super(props); 

    this.state = { 
     posts:"hello" 
    }; 
    } 
componentWillMount() { 
    axios.get(`http://192.168.1.9:8082`) 
     .then(response => {this.setState({ posts}); 

     }); 
    } 


    render() { 
    return (
     <div> 
     <h1>{this.state.posts}</h1> 


     </div> 
    ); 
    } 
} 


export default App; 
+0

エラーは何ですか?それも共有してください – Swapnil

+0

「投稿」が定義されていないためです。おそらく 'this.setState({posts:response.data})'のようなものを作るべきでしょうか? – Ziumin

+0

提案されているように、response.data.postsではなく、レスポンスにpostsプロパティがありますか? – Pineda

答えて

1

をあなたは、いくつかの誤植を持っている。

1。

.then(response => {this.setState({ post }) 

どこpostは私が

2推測response.postようなものになるでしょうか?である。

this.state = { 
    posts:"hello" 
}; 
... 
this.setState({ post }) 

postまたはposts

3。

<h1> {this.response}</h1> 

this.responseとは何ですか?あなたは決してそれを宣言しません。

これらのタイプミスを修正してください。動作するはずです。

+0

ああ、ごめんなさい。this.setState({posts}) –

+0

サーバーからデータを受信して​​いますが、表示できません。私はこの部分で間違っていると思う "。(応答=> {this.setState({投稿});" –

+0

私は(最初のもの)と同じように、投稿は全く存在しません。 'response.data.posts'または' response.posts' – CodinCat

関連する問題