2017-06-15 7 views
-1

私はReactを学ぼうとしています。私はJavascriptに関しては初心者です。今、私はFlickrのAPIからデータを取得しているアプリケーションに取り組んでいます。問題は、Main.jsコンポーネントのプロップでマップメソッドを使用しようとすると、「Uncaught TypeError:this.props.photos.mapが関数ではない」というエラーが表示されることです。 Stackoverflowでここで検索した後、私は問題がthis.propsが配列ではなくjavascriptオブジェクトであると思います。問題は、それを配列にする方法を理解できないことです。誰も私が間違っていることを説明することはできますか?React .mapは関数ではありません

マイコード:

class App extends Component { 

    constructor() { 
    super(); 
    this.state = { 

    } 
} 

componentDidMount() { 

let apiKey = 'xxxxxxxxxxxxxxxxxx'; 
let searchKeyword = 'nature'; 
let url = `https://api.flickr.com/services/ 
      rest/?api_key=${apiKey}&method=flickr.photos. 
      search&format=json&nojsoncallback=1&&per_page=50 
      &page=1&text=${searchKeyword}`; 

fetch(url) 
    .then(response => response.json()) 
    .then(data => data.photos.photo.map((x) => { 

    this.setState({ 
     farm: x.farm, 
     id: x.id, 
     secret: x.secret, 
     server: x.server}) 
    // console.log(this.state) 
    })) 
} 

    render() { 
     return (
     <div className="App"> 
      <Header /> 
      <Main img={this.state.photos} /> 
      <Navigation /> 
     </div> 
    ); 
    } 
    } 

    export default class Main extends Component { 

    render() { 

    return(
     <main className="main"> 
     {console.log(this.props.photos)} 
     </main> 
    ) 
    } 
} 

編集: なぜthis.props.img未定義の最初のですか?

Screen shot from console.log(this.props.img)

+0

「写真」とは何ですか? 'map'は' Array'のメソッドです。 –

+0

* 'Uncaught TypeError:this.props.photos.mapは関数ではありません。' 'というエラーが表示されます。' * 'this.props.photos.map'は引用符で囲まれたコードのどこにも表示されません。 'data.photos.photo.map'は、それを意味しましたか? –

+1

別名: 'map'を呼び出していて、' map'コールバック内で 'setState'を呼び出していて値を返さないので、すべてのエントリを' undefined'にマッピングしています。あなたは 'map'の戻り値を使っていません(あなたは' then'からそれを返していますが、何もその約束を使用しないので、未使用になります)。ほとんどの場合、 'setState'を繰り返し呼び出すことはほとんどありません。 –

答えて

2
fetch(url) 
    .then(response => response.json()) 
    .then(data => data.photos.photo.map((x) => { 

    this.setState({ 
     farm: x.farm, 
     id: x.id, 
     secret: x.secret, 
     server: x.server}) 
    })) 

何が起こっているあなたの約束であなたのマップ機能が返されるすべての写真をコンポーネントの状態をリセットしていることです。あなたの状態は返された写真のリストの中の最後のオブジェクトになります。

ここで私はあなたが何をしたいのか

const testArray = [1,2,3,4]; 

let currentState; 

testArray.map((value) => currentState = value) 

console.log(currentState); 

を参照しています何のより単純化した例は、あなたが達成しようとしているもの、あなたがあなたの状態を希望の場合、この

const testArray = [1,2,3,4]; 

let currentState; 

//Notice we are using the return value of the map function itself. 
currentState = testArray.map((value) => value) 

console.log(currentState); 

ですマップ関数の結果になります(マップから結果の配列を返します)。このようなもの:

fetch(url) 
    .then(response => response.json()) 
    .then(data => 
    this.setState({ 
     photos: 
     data.photos.photo.map((x) => ({ 
      farm: x.farm, 
      id: x.id, 
      secret: x.secret, 
      server: x.server 
     })) 
    }) 
    ) 
+0

ありがとうございました。メインコンポーネントのthis.propsでマップメソッドを使用しようとすると、エラーメッセージ「マップは関数ではありません」が表示されます。私がしたいことは、this.stateのすべてのオブジェクトに対して新しいimg-tagを作成することです。何 <メインクラス名= "メイン"> \t {this.props.photos.map((X)=> { \tリターン \t})} – Tose

+0

もしthis.props.photosコンソール場合、それは戻る? –

+0

オブジェクトのリスト。それは配列ではないという問題ですか? オブジェクト{0:オブジェクト1:オブジェクト2:オブジェクト3:オブジェクト4:オブジェクト5:オブジェクト6:オブジェクト7:オブジェクト8:オブジェクト9:オブジェクト10:オブジェクト11:オブジェクト、12:オブジェクト、13:オブジェクト、14:オブジェクト、15:オブジェクト、16:オブジェクト、17:オブジェクト、18:オブジェクト、19:オブジェクト、20:オブジェクト、21:オブジェクト、22:オブジェクト、23: 24:オブジェクト25:オブジェクト26:オブジェクト27:オブジェクト28:オブジェクト29:オブジェクト30:オブジェクト31:オブジェクト32:オブジェクト33:オブジェクト34:オブジェクト35:オブジェクト36:オブジェクト、37:オブジェクト、38:オブジェクト} – Tose

関連する問題