简体   繁体   English

React-Redux:接收和发送表单数据(服务器)

[英]React-Redux: Receive and send form data (server)

I ask for help how to make devextreme form on redux.我寻求帮助如何在 redux 上制作 devextreme 形式。 No plugins: redux-form, formik and other.无插件:redux-form、formik 等。 It seemed to me that they would be unnecessary, because Devextreme has many built-in features.在我看来,它们是不必要的,因为 Devextreme 有许多内置功能。 Maybe I'm wrong.也许我错了。

I read this article, but maybe there is something simpler https://medium.com/@pnpsegonne/tutorial-creating-a-form-with-react-redux-c1b3025cf26b我读了这篇文章,但也许​​有更简单的东西https://medium.com/@pnpsegonne/tutorial-creating-a-form-with-react-redux-c1b3025cf26b

I need an example of how to get data from the server into the form fields.我需要一个如何从服务器获取数据到表单字段的示例。 And how to change them and transfer them to the server by click on "Save" button.以及如何通过单击“保存”按钮更改它们并将它们传输到服务器。 Of course, for example, the server can be replaced with a timeout function or something.当然,比如服务器可以换成超时功能什么的。 https://codesandbox.io/s/overview-devextreme-forms-and-multi-purpose-t86mw https://codesandbox.io/s/overview-devextreme-forms-and-multi-purpose-t86mw

import React from "react";

import { employee, positions, states } from "./data.js";

import Form, {
  SimpleItem,
  GroupItem,
  ButtonItem,
  Label
} from "devextreme-react/form";
import "devextreme-react/text-area";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.birthDateOptions = { width: "100%" };
    this.positionOptions = {
      items: positions,
      value: ""
    };
    this.stateOptions = {
      items: states
    };
    this.phoneOptions = { mask: "+1 (000) 000-0000" };
    this.notesOptions = { height: 140 };
  }
  render() {
    return (
      <Form formData={employee}>
        <GroupItem cssClass="first-group" colCount={4}>
          <SimpleItem render={avatarRender} />
          <GroupItem colSpan={3}>
            <SimpleItem dataField="FirstName" />
            <SimpleItem dataField="LastName" />
            <SimpleItem
              dataField="BirthDate"
              editorType="dxDateBox"
              editorOptions={this.birthDateOptions}
            />
          </GroupItem>
        </GroupItem>
        <GroupItem cssClass="second-group" colCount={2}>
          <GroupItem>
            <SimpleItem dataField="Address" />
            <SimpleItem dataField="City" />
            <SimpleItem
              dataField="Position"
              editorType="dxSelectBox"
              editorOptions={this.positionOptions}
            />
          </GroupItem>
          <GroupItem>
            <SimpleItem
              dataField="State"
              editorType="dxSelectBox"
              editorOptions={this.stateOptions}
            />
            <SimpleItem dataField="ZipCode" />
            <SimpleItem dataField="Mobile" editorOptions={this.phoneOptions}>
              <Label text="Phone" />
            </SimpleItem>
          </GroupItem>
          <SimpleItem
            colSpan={2}
            dataField="Notes"
            editorType="dxTextArea"
            editorOptions={this.notesOptions}
          />
        </GroupItem>
        <ButtonItem
          horizontalAlignment="left"
          buttonOptions={{
            text: "Save",
            type: "success",
            useSubmitBehavior: true
          }}
        />
      </Form>
    );
  }
}

function avatarRender() {
  return <div className="form-avatar" />;
}

export default App;

I need an example of how to get data from the server into the form fields.我需要一个如何从服务器获取数据到表单字段的示例。 And how to change them and transfer them to the server by click on "Save" button.以及如何通过单击“保存”按钮更改它们并将它们传输到服务器。 Of course, for example, the server can be replaced with a timeout function or something.当然,比如服务器可以换成超时功能什么的。

If this is the only thing that matters you then "you might not need redux".如果这是唯一对您重要的事情,那么“您可能不需要 redux”。 You can just call the API in your componentDidMount and setState with the desired data returned from the API.您可以使用从 API 返回的所需数据调用componentDidMountsetState的 API。

but maybe there is something simpler但也许有更简单的东西

Maybe not.也许不吧。 Don't worry it will get easier with time.别担心,随着时间的推移,它会变得更容易。

For now, I have implemented API integration using redux and redux-saga here .目前,我已经在这里使用 redux 和 redux-saga 实现了 API 集成。 Hope this is all you need.希望这就是你所需要的。

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

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