最終更新:2022-12-16 (金) 03:14:31 (490d)
React/フォーム
https://ja.reactjs.org/docs/forms.html
状態
- React の state を “信頼できる唯一の情報源 (single source of truth)” とする
- Reactのstate
- DOMのコントロールの状態
コントロール
例
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> ); } }