表单作为具有反应的功能组件 [英] Forms as functional components with react
问题描述
如果我想创建一个包含表单的功能组件,例如登录,并且我想包含 App 组件中的状态并且 Login 将是它的子组件,我可以使用中的表单来改变 App 中的状态吗?登录孩子?
If I want to make a functional component that will contain a form ,to login for example, and I want to contain the state in App component and Login will be its child, can I mutate the state in App using the form in Login child?
推荐答案
是的.将两个 props 一个对象 data
和一个方法 onChange
从 App
传递给 Login
.data
将在 Login
中设置表单的值.
Yes. Pass two props an object data
and a method onChange
to Login
from App
.
data
will set the values for the form in Login
.
如果 Login
表单有任何更改,则使用更新的表单值触发 onChange
.
Fire onChange
with updated form values if there is any change in Login
form.
在 App
中处理它并更新其状态,然后将其作为 data
向下传递到 Login
.
Handle it in App
and update its state which will then flow down to Login
as data
.
这篇关于表单作为具有反应的功能组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!