event.preventDefault
定義と使用法:
イベントのデフォルトのアクションをキャンセルします
次のコードを簡単に見てください。
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {
value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({
value: event.target.value});
}
handleSubmit(event) {
alert('A name was submitted: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={
this.handleSubmit}>
<label>
Name:
<input type="text" value={
this.state.value} onChange={
this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
}
これは私が公式のReactウェブサイトのドキュメントから取ったREACTです
注意が必要なのは、onSubmit部分のコードです。
handleSubmit関数で、event.preventDefault()が追加されていることがわかります。
この場合、元のonSubmitのデフォルトの効果をキャンセルします。
効果は何ですか?URLに 'が含まれていますか?'変換ページの効果