news 2026/4/23 15:11:22

本章节我们将讨论如何在 React 中使用表单。 DOM

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
本章节我们将讨论如何在 React 中使用表单。 DOM

React 表单与事件

本章节我们将讨论如何在 React 中使用表单。

HTML 表单元素与 React 中的其他 DOM 元素有所不同,因为表单元素生来就保留一些内部状态。

在 HTML 当中,像 <input>, <textarea>, 和 <select> 这类表单元素会维持自身状态,并根据用户输入进行更新。但在React中,可变的状态通常保存在组件的状态属性中,并且只能用 setState() 方法进行更新。

一个简单的实例

在实例中我们设置了输入框 input 值value = {this.state.data}。在输入框值发生变化时我们可以更新 state。我们可以使用onChange事件来监听 input 的变化,并修改 state。

React 实例

class HelloMessage extends React.Component { constructor(props) { super(props); this.state = {value: 'Hello Runoob!'}; this.handleChange = this.handleChange.bind(this); } handleChange(event) { this.setState({value: event.target.value}); } render() { var value = this.state.value; return <div> <input type="text" value={value} onChange={this.handleChange} /> <h4>{value}</h4> </div>; } } const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <HelloMessage /> );

上面的代码将渲染出一个值为 Hello Runoob! 的 input 元素,并通过 onChange 事件响应更新用户输入的值。

实例 2

在以下实例中我们将为大家演示如何在子组件上使用表单。onChange方法将触发 state 的更新并将更新的值传递到子组件的输入框的value上来重新渲染界面。

你需要在父组件通过创建事件句柄 (handleChange) ,并作为 prop (updateStateProp) 传递到你的子组件上。

React 实例

class Content extends React.Component { render() { return ( <div> <input type="text" value={this.props.myDataProp} onChange={this.props.updateStateProp} /> <h4>{this.props.myDataProp}</h4> </div> ); } } class HelloMessage extends React.Component { constructor(props) { super(props); this.state = { value: 'Hello Runoob!' }; this.handleChange = this.handleChange.bind(this); } handleChange(event) { this.setState({ value: event.target.value }); } render() { var value = this.state.value; return ( <div> <Content myDataProp={value} updateStateProp={this.handleChange} /> </div> ); } } const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<HelloMessage />);

Select 下拉单

在 React 中,不使用 selected 属性,而在根 select 标签上用 value 属性来表示选中项。

React 实例

class FlavorForm extends React.Component { constructor(props) { super(props); this.state = {value: 'coconut'}; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(event) { this.setState({value: event.target.value}); } handleSubmit(event) { alert('Your favorite flavor is: ' + this.state.value); event.preventDefault(); } render() { return ( <form onSubmit={this.handleSubmit}> <label> 选择您最喜欢的网站 <select value={this.state.value} onChange={this.handleChange}> <option value="gg">Google</option> <option value="rn">Runoob</option> <option value="tb">Taobao</option> <option value="fb">Facebook</option> </select> </label> <input type="submit" value="提交" /> </form> ); } } const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <FlavorForm /> );

多个表单

当你有处理多个 input 元素时,你可以通过给每个元素添加一个 name 属性,来让处理函数根据 event.target.name 的值来选择做什么。

React 实例

class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoing: true, numberOfGuests: 2 }; this.handleInputChange = this.handleInputChange.bind(this); } handleInputChange(event) { const target = event.target; const value = target.type === 'checkbox' ? target.checked : target.value; const name = target.name; this.setState({ [name]: value }); } render() { return ( <form> <label> 是否离开: <input name="isGoing" type="checkbox" checked={this.state.isGoing} onChange={this.handleInputChange} /> </label> <br /> <label> 访客数: <input name="numberOfGuests" type="number" value={this.state.numberOfGuests} onChange={this.handleInputChange} /> </label> </form> ); } }


React 事件

以下实例演示通过 onClick 事件来修改数据:

React 实例

class HelloMessage extends React.Component { constructor(props) { super(props); this.state = {value: 'Hello Runoob!'}; this.handleChange = this.handleChange.bind(this); } handleChange(event) { this.setState({value: '菜鸟教程'}) } render() { var value = this.state.value; return <div> <button onClick={this.handleChange}>点我</button> <h4>{value}</h4> </div>; } } const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <HelloMessage /> );

当你需要从子组件中更新父组件的state时,你需要在父组件通过创建事件句柄 (handleChange) ,并作为 prop (updateStateProp) 传递到你的子组件上。实例如下:

https://www.bilibili.com/read/cv45004058
https://www.bilibili.com/opus/1161541693007200259
https://www.bilibili.com/read/cv45004058/
https://www.bilibili.com/opus/1161541693007200259/
https://www.bilibili.com/read/cv45004057
https://www.bilibili.com/opus/1161541693014540291
https://www.bilibili.com/read/cv45004057/
https://www.bilibili.com/opus/1161541693014540291/
https://www.bilibili.com/read/cv45004049
https://www.bilibili.com/opus/1161541576989605894
https://www.bilibili.com/read/cv45004049/
https://www.bilibili.com/opus/1161541576989605894/
https://www.bilibili.com/read/cv45004048
https://www.bilibili.com/opus/1161541576999043074
https://www.bilibili.com/read/cv45004048/
https://www.bilibili.com/opus/1161541576999043074/
https://www.bilibili.com/read/cv45004047
https://www.bilibili.com/opus/1161541572726095872
https://www.bilibili.com/read/cv45004047/
https://www.bilibili.com/opus/1161541572726095872/
https://www.bilibili.com/read/cv45004035
https://www.bilibili.com/opus/1161541357989265413
https://www.bilibili.com/read/cv45004034
https://www.bilibili.com/opus/1161541353695346688
https://www.bilibili.com/read/cv45004033
https://www.bilibili.com/opus/1161541353663889428
https://www.bilibili.com/read/cv45004032
https://www.bilibili.com/opus/1161541349356339209
https://www.bilibili.com/read/cv45004031
https://www.bilibili.com/opus/1161541349384650776
https://www.bilibili.com/read/cv45004007
https://www.bilibili.com/opus/1161540876956073992
https://www.bilibili.com/read/cv45004002
https://www.bilibili.com/opus/1161540799605768198
https://www.bilibili.com/read/cv45004001
https://www.bilibili.com/opus/1161540799652954113
https://www.bilibili.com/read/cv45004120
https://www.bilibili.com/opus/1161542436019765256
https://www.bilibili.com/read/cv45004120/
https://www.bilibili.com/opus/1161542436019765256/
https://www.bilibili.com/read/cv45004119
https://www.bilibili.com/opus/1161542431703826465
https://www.bilibili.com/read/cv45004119/
https://www.bilibili.com/opus/1161542431703826465/
https://www.bilibili.com/read/cv45004118
https://www.bilibili.com/opus/1161542431703826455
https://www.bilibili.com/read/cv45004118/
https://www.bilibili.com/opus/1161542431703826455/
https://www.bilibili.com/read/cv45004117
https://www.bilibili.com/opus/1161542431725846528
https://www.bilibili.com/read/cv45004117/
https://www.bilibili.com/opus/1161542431725846528/
https://www.bilibili.com/read/cv45004116
https://www.bilibili.com/opus/1161542431715360768
https://www.bilibili.com/read/cv45004116/
https://www.bilibili.com/opus/1161542431715360768/
https://www.bilibili.com/read/cv45004110
https://www.bilibili.com/opus/1161542285687521296
https://www.bilibili.com/read/cv45004110/
https://www.bilibili.com/opus/1161542285687521296/
https://www.bilibili.com/read/cv45004109
https://www.bilibili.com/opus/1161542285669695496
https://www.bilibili.com/read/cv45004109/
https://www.bilibili.com/opus/1161542285669695496/
https://www.bilibili.com/read/cv45004108
https://www.bilibili.com/opus/1161542285703249928
https://www.bilibili.com/read/cv45004108/
https://www.bilibili.com/opus/1161542285703249928/
https://www.bilibili.com/read/cv45004107
https://www.bilibili.com/opus/1161542285680181254
https://www.bilibili.com/read/cv45004107/
https://www.bilibili.com/opus/1161542285680181254/
https://www.bilibili.com/read/cv45004106
https://www.bilibili.com/opus/1161542281390456857
https://www.bilibili.com/read/cv45004106/
https://www.bilibili.com/opus/1161542281390456857/
https://www.bilibili.com/read/cv45004105
https://www.bilibili.com/opus/1161542281420865568
https://www.bilibili.com/read/cv45004105/
https://www.bilibili.com/opus/1161542281420865568/
https://www.bilibili.com/read/cv45004104
https://www.bilibili.com/opus/1161542281395699717
https://www.bilibili.com/read/cv45004104/
https://www.bilibili.com/opus/1161542281395699717/
https://www.bilibili.com/read/cv45004103
https://www.bilibili.com/opus/1161542281404088329
https://www.bilibili.com/read/cv45004103/
https://www.bilibili.com/opus/1161542281404088329/
https://www.bilibili.com/read/cv45004102
https://www.bilibili.com/opus/1161542281391505417
https://www.bilibili.com/read/cv45004102/
https://www.bilibili.com/opus/1161542281391505417/
https://www.bilibili.com/read/cv45004096
https://www.bilibili.com/opus/1161542199787126786
https://www.bilibili.com/read/cv45004096/
https://www.bilibili.com/opus/1161542199787126786/
https://www.bilibili.com/read/cv45004094
https://www.bilibili.com/opus/1161542191201386516
https://www.bilibili.com/read/cv45004094/
https://www.bilibili.com/opus/1161542191201386516/

React 实例

class Content extends React.Component { render() { return <div> <button onClick = {this.props.updateStateProp}>点我</button> <h4>{this.props.myDataProp}</h4> </div> } } class HelloMessage extends React.Component { constructor(props) { super(props); this.state = {value: 'Hello Runoob!'}; this.handleChange = this.handleChange.bind(this); } handleChange(event) { this.setState({value: '菜鸟教程'}) } render() { var value = this.state.value; return <div> <Content myDataProp = {value} updateStateProp = {this.handleChange}></Content> </div>; } } const root = ReactDOM.createRoot(document.getElementById("root")); root.render( <HelloMessage /> );

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 12:16:28

解决黑屏报错!Open-AutoGLM敏感屏幕处理方法

解决黑屏报错&#xff01;Open-AutoGLM敏感屏幕处理方法 你是否在运行 Open-AutoGLM 时&#xff0c;突然看到这样一行提示&#xff1a; 屏幕被标记为敏感屏幕&#xff08;黑屏&#xff09;&#xff0c;这可能是由于应用正在加载中或设备安全设置导致的。 根据安全规则&#xf…

作者头像 李华
网站建设 2026/4/11 1:06:57

Cherry Studio命令行工具高效管理指南:零基础入门到精通

Cherry Studio命令行工具高效管理指南&#xff1a;零基础入门到精通 【免费下载链接】cherry-studio &#x1f352; Cherry Studio is a desktop client that supports for multiple LLM providers. Support deepseek-r1 项目地址: https://gitcode.com/GitHub_Trending/ch/c…

作者头像 李华
网站建设 2026/4/23 13:02:21

开源富文本编辑器:轻量化解决方案的技术测评

开源富文本编辑器&#xff1a;轻量化解决方案的技术测评 【免费下载链接】ueditor rich text 富文本编辑器 项目地址: https://gitcode.com/gh_mirrors/ue/ueditor 在现代Web应用开发中&#xff0c;富文本编辑器作为内容创作的核心工具&#xff0c;其性能表现与功能完整…

作者头像 李华
网站建设 2026/4/23 13:44:12

开箱即用的图像修复方案:fft npainting lama体验报告

开箱即用的图像修复方案&#xff1a;FFT NPainting LaMa体验报告 在日常图像处理中&#xff0c;我们常遇到水印遮挡、无关物体干扰、照片瑕疵等困扰——传统PS手动修复耗时费力&#xff0c;专业工具学习成本高&#xff0c;而多数AI修复工具又依赖复杂配置或云端服务。最近试用…

作者头像 李华
网站建设 2026/4/23 12:14:41

三步掌握时间序列预测工具从零到一环境部署与参数调优全攻略

三步掌握时间序列预测工具从零到一环境部署与参数调优全攻略 【免费下载链接】prophet Tool for producing high quality forecasts for time series data that has multiple seasonality with linear or non-linear growth. 项目地址: https://gitcode.com/gh_mirrors/pro/p…

作者头像 李华
网站建设 2026/4/23 13:59:26

轻量级图像分割模型高效训练指南:从MobileSAM到移动端部署

轻量级图像分割模型高效训练指南&#xff1a;从MobileSAM到移动端部署 【免费下载链接】MobileSAM This is the official code for MobileSAM project that makes SAM lightweight for mobile applications and beyond! 项目地址: https://gitcode.com/gh_mirrors/mo/MobileS…

作者头像 李华