在ReactJS中什么是受控组件和非受控组件?它们之间有什么不同?
当前回答
这与有状态DOM组件(表单元素)有关,React文档解释了其中的区别:
A Controlled Component is one that takes its current value through props and notifies changes through callbacks like onChange. A parent component "controls" it by handling the callback and managing its own state and passing the new values as props to the controlled component. You could also call this a "dumb component". A Uncontrolled Component is one that stores its own state internally, and you query the DOM using a ref to find its current value when you need it. This is a bit more like traditional HTML.
大多数原生React表单组件同时支持受控和不受控使用:
// Controlled:
<input type="text" value={value} onChange={handleChange} />
// Uncontrolled:
<input type="text" defaultValue="foo" ref={inputRef} />
// Use `inputRef.current.value` to read the current value of <input>
在大多数(或所有)情况下,您应该使用受控组件。
其他回答
这与有状态DOM组件(表单元素)有关,React文档解释了其中的区别:
A Controlled Component is one that takes its current value through props and notifies changes through callbacks like onChange. A parent component "controls" it by handling the callback and managing its own state and passing the new values as props to the controlled component. You could also call this a "dumb component". A Uncontrolled Component is one that stores its own state internally, and you query the DOM using a ref to find its current value when you need it. This is a bit more like traditional HTML.
大多数原生React表单组件同时支持受控和不受控使用:
// Controlled:
<input type="text" value={value} onChange={handleChange} />
// Uncontrolled:
<input type="text" defaultValue="foo" ref={inputRef} />
// Use `inputRef.current.value` to read the current value of <input>
在大多数(或所有)情况下,您应该使用受控组件。
受控制的组件不保持它们的状态。
它们需要的数据从父组件传递给它们。
它们通过回调函数与这些数据交互,回调函数也从父进程传递给子进程。
受控组件:表单数据由React组件处理。
ReactJS中受控组件的流程 图像
不受控组件:表单数据由DOM本身处理。
ReactJS中未受控组件的流 图像
受控组件是从回调函数获取更改值的组件 而且 未受控组件是拥有来自DOM的组件。 例如, 当输入值发生变化时,我们可以使用受控组件中的onChange函数 我们也可以使用像ref这样的DOM来获取值。
TLDR;
https://www.youtube.com/watch?v=6L2Rd116EvY 你可以查看那一页,他解释得很好.......
控制组件
受控组件是呈现表单元素的组件,例如<input/>,其值由react和react单独控制。例如,复制下面的代码并尝试在DOM中更改输入字段…
export default function Component() {
return (
<div>
<input type="text" value="somevalue" />
</div>
)
}
无论你如何尝试更新上面输入的值,react都不会让你这么做。因为react想要成为一个使用状态来控制更新值的人,因此title controlled…
它的值可以通过连接属性onChange和value到一个状态来更新,如下所示。
function Component() {
const [text,setText] = React.useState("")
return (
<div>
<input type="text" onChange={(e)=>setText(e.target.value)} value={text} />
<p>{text}</p>
</div>
)
}
现在我们的输入和它的值可以被更新,用来渲染一些东西或执行即时验证....
不受控制的组件
非受控组件是呈现<input/>这样的表单元素的组件,其值可以由Dom元素处理,受控和非受控之间的一个主要区别是值属性定义。对于不受控制的,我们有一个defaultValue代替或根本没有值。
function Component() {
return (
<div>
<input type="email" id="message" defaultValue="example@mail.com" />
</div>
)
}
上面输入的值可以被改变,并由DOM控制,而不需要React…
建议在react中更多地使用受控组件,因为您可以执行即时验证和强制动态输入。
推荐文章
- js:将一个组件包装成另一个组件
- 在typescript中一直使用。tsx而不是。ts有什么缺点吗?
- TypeScript React应用程序中的PropTypes
- {this.props是什么?孩子们,什么时候应该使用它?
- 反应。useState不会从props中重新加载状态
- 在Link react-router中传递道具
- React不会加载本地图像
- Axios获取url工作,但第二个参数作为对象,它不
- 我如何使用多个引用的元素与挂钩数组?
- 组件中useState对状态更新器的多次调用会导致多次重渲染
- 什么是useState()在React?
- 如何使webpack开发服务器运行在端口80和0.0.0.0使其公开访问?
- 无法找到模块“react-materialize”的声明文件。` path/to/module-name.js `隐式具有any类型
- 如何从子组件内部更新React上下文?
- 不能在呈现不同组件警告时更新组件