
- 不受控制的组件
这种模式意味着 react 不控制表单数据,dom 保存表单状态。
访问 dom 时,必须使用 useref 钩子设置 ref 属性。
・src/components/uncontrol-form.jsx
import react from "react";
export const uncontrolledform = () => {
const nameinputref = react.createref();
const ageinputref = react.createref();
console.log("renedering");
const submitform = (e) => {
console.log(nameinputref.current.value);
console.log(ageinputref.current.value);
e.preventdefault();
};
return (
);
};
- 受控组件
此模式意味着 react 使用 usestate 钩子控制表单数据。
NetShop软件特点介绍: 1、使用ASP.Net(c#)2.0、多层结构开发 2、前台设计不采用任何.NET内置控件读取数据,完全标签化模板处理,加快读取速度3、安全的数据添加删除读取操作,利用存储过程模式彻底防制SQL注入式攻击4、前台架构DIV+CSS兼容IE6,IE7,FF等,有利于搜索引挚收录5、后台内置强大的功能,整合多家网店系统的功能,加以优化。6、支持三种类型的数据库:Acces
我们可以完全控制输入值并实时更新。
import React, { useEffect, useState } from "react";
export const ControlledForm = () => {
const [errorMessage, setErrorMessage] = useState("");
const [name, setName] = useState("");
const [age, setAge] = useState();
useEffect(() => {
if (name.length < 1) {
setErrorMessage("name can not be empty");
} else {
setErrorMessage("");
}
}, [name]);
return (
);
};









