私のアプリでlast.fm APIが動作しています。私は関連するアーティストをつかんでいます。だからアイデアはアーティストを検索し、関連するアーティストのリストを返します。React.js - onChange undefined
私は 'onClick'を使用すると、入力値を取得するので完璧に機能しますが、 'onChange'を使用したいので間違った結果を返すようです。私はそれがいくつかのステージや何かで未定義だからだと思う!
これを修正する方法はありますか?
// AJAX
import axios from "axios";
module.exports = {
fetchArtistCategory: (artist) => {
let key = "12345";
let encodedURI = window.encodeURI('http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=' + artist + '&api_key=' + key + '&format=json');
return axios.get(encodedURI)
.then((res) => {
return res
});
}
}
//アプリケーション
import React from "react";
import {render} from "react-dom";
import Artists from "./components/Artists.js";
import Api from "./components/Api.js";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
artist: [],
}
this.searchArtist = this.searchArtist.bind(this);
}
componentDidMount() {
}
searchArtist() {
Api.fetchArtistCategory(this.searchArtists.value)
.then((res) => {
this.setState({
artist: res.data.similarartists.artist
});
})
}
render() {
return (
<div>
<Artists artistValue={this.state.artist} />
<input type="text" placeholder="Search artist" ref={(ref) => this.searchArtists = ref} onChange={this.searchArtist} />
<input type="submit"/>
</div>
)
}
}
render(<App />, document.getElementById("main"));
'this.searchArtist'メソッドの中の' console.log(this.searchArtists) 'の出力は何ですか? – webdeb