0
林不知道我在做什麼錯在這裏。Axios請求失敗反應
我希望Main
組件通過ajax請求方法傳遞給子組件InputForm
,它將返回將存儲在Main
組件狀態中的結果。
class Main extends React.Component {
constructor(props){
super(props);
this.state = {
fetching : false,
repos : {}
};
this.getGitHubRepo = this.getGitHubRepo.bind(this);
}
getGitHubRepo(user){
this.setState({ fetching : true });
console.log("form submitted!", user);
axios.get(user)
.then((response) => {
console.log("response =>", response);
})
.catch((error) => {
console.log("error => ", error);
});
}
render(){
return(
<div className = "appContainer">
<InputForm getGitHubRepo = { this.getGitHubRepo } />
</div>
);
}
}
class InputForm extends React.Component{
constructor(props){
super(props);
this.state = {
inputValue : "",
};
this.recordInput = this.recordInput.bind(this);
}
recordInput(e){
this.setState({ inputValue : e.target.value });
}
render(){
let getPath = `https://api.github.com/${this.state.inputValue}`;
return(
<form onSubmit = {() => this.props.getGitHubRepo(getPath)}>
<label htmlFor = "user_input">
Github Username
</label>
<input id = "user_input"
type = "input"
onChange = { this.recordInput } />
<input type = "submit" value = "get repos" />
</form>
);
}
}
ReactDOM.render(<Main />, document.getElementById("app"));
我沒有得到任何結果&我的WebPack服務器刷新頁面。
謝謝@Canastro。我沒有意識到preventDefault。 – Kayote