我在應用程序中使用了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