0
ここで間違っていることはわかりません。Axiosでのリクエストの失敗
コンポーネントを、子コンポーネントInputForm
にajaxリクエストメソッドで渡して、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