简体   繁体   English

大型React-Redux表单应用

[英]Large react-redux form app

I've been working on rewriting one of the modules in my company using react, the module is a single page composable of 4-5 different forms, selections made in each form are then determine the appeareance of the next form step. 我一直在使用react重写公司中的一个模块,该模块是一个由4-5种不同形式组成的页面,在每种形式中进行的选择将确定下一个表单步骤的出现。

There are a lot of "static" input fields which not affecting the visual ui state of the app, but are required to send to the server, other inputs are changing the ui state. 有许多“静态”输入字段不会影响应用程序的视觉ui状态,但需要发送到服务器,其他输入则会更改ui状态。

I'm looking for the right approach for this type of apps, since it seems attaching onChange event to every single input (there are more than 100 inputs total in the whole page). 我正在为这种类型的应用程序寻找正确的方法,因为它似乎将onChange事件附加到每个单个输入(整个页面中总共有100多个输入)。 I've used react-redux-forms plugin, but it is too much of a blockbox for me, since I need to have direct access to the state and make decision based on it. 我已经使用了react-redux-forms插件,但是对我来说,它实在是太多了,因为我需要直接访问状态并根据状态进行决策。 I would preferer hacing more control over the state. 我希望对状态有更多的控制。

Is the right solution to bind onChange event for every input? 是为每个输入绑定onChange事件的正确解决方案吗? Or is there a better approach. 还是有更好的方法。

We do this with redux-form quite easily. 我们使用redux-form很容易做到这一点。 Because everything's maintained in the fields prop, you could do something like this: 由于所有内容都在fields属性中维护,因此您可以执行以下操作:

const Form = ({
  fields,
  handleSubmit,
  saveForm
}) => (
  <form onSubmit={handleSubmit(saveForm)}>
    <fieldset>
      <input type="text" {...fields.hasAlternativeDelivery} />
    </fieldset>
    {fields.hasAlternativeDelivery.value === true &&
      <fieldset>
        {/* more fields go here */}
      </fieldset>
    }
  </form>
);

We then conditionally validate certain fieldsets like this . 然后,我们有条件地验证某些像这样的字段集。 So, to answer your question: you shouldn't rely on change events to hide / show certain fields, this goes against the very nature of React (React is declarative, what you're describing is an imperative way of doing things). 因此,回答您的问题:您不应该依赖更改事件来隐藏/显示某些字段,这违背了React的本质(React是声明性的,您所描述的是一种强制性的处理方式)。 Instead, figure out what state (/props) should lead to which UI. 相反,找出什么状态(/ props)应该导致哪个UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM