【问题标题】:React JS shortern attributesReact JS 缩短属性
【发布时间】:2020-06-11 06:26:56
【问题描述】:

我正在研究 React Js,我遇到了一个问题,即我需要存储大量的值/属性。 我的代码如下:

import React from 'react';
import axios from 'axios';
import '../Container/Company.css'
import { Container, Col, Form, Row, FormGroup, Label, Input, Button } from 'reactstrap';
class AddCompany extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      CompanyName: '',
      CompanyReg: '',
      CompanyAddr1: '',
      CompanyAddr2: '',
      CompanyAddr3: '',
      City: '',
      State: '',
      PostCode: '',
      Phone: '',
      Fax: '',
      ContactPerson: '',
      Email: '',
      DealerName: '',
      Phone2: '',
      Country: '',
      Remark: '',
      DealerCode: '',
      SerialNo: '',
      IsActive: '',
    }
  }
  AddCompany = () => {
    axios.post('http://localhost:57025/api//companyedit/', {
      CompanyName: this.state.CompanyName,
      CompanyReg: this.state.CompanyReg,
      CompanyAddr1: this.state.CompanyAddr1,
      CompanyAddr2: this.state.CompanyAddr2,
      CompanyAddr3: this.state.CompanyAddr3,
      City: this.state.City,
      PostCode: this.state.PostCode,
      Phone: this.state.Phone,
      Fax: this.state.Fax,
      ContactPerson: this.state.ContactPerson,
      Email: this.state.Email,
      DealerName: this.state.DealerName,
      Phone2: this.state.Phone2,
      Country: this.state.Country,
      Remark: this.state.Remark,
      DealerCode: this.state.DealerCode,
      SerialNo: this.state.SerialNo,
      IsActive: this.state.IsActive})
      .then(json => {
        if (json.data.Status === 'Success') {
          console.log(json.data.Status);
          alert("Data Save Successfully");
          this.props.history.push('/CompanyList')
        }
        else {
          alert('Data not Saved');
          debugger;
          this.props.history.push('/CompanyList')
        }
      })
  }

  handleChange = (e) => {
    this.setState({ [e.target.name]: e.target.value });
  }

  render() {
    return (
      <Container className="App">
        <h4 className="PageHeading">Enter Company Informations</h4>
        <Form className="form">
          <Col>
            <FormGroup row>
              <Label for="CompanyName" sm={2}>CompanyName</Label>
              <Col sm={10}>
                <Input type="text" CompanyName="CompanyName" onChange={this.handleChange} value={this.state.CompanyName} placeholder="Enter Name" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="CompanyReg" sm={2}>CompanyReg</Label>
              <Col sm={10}>
                <Input type="text" CompanyReg="CompanyReg" onChange={this.handleChange.CompanyReg} value={this.state.CompanyReg} placeholder="Enter Reg" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="CompanyAddr1" sm={2}>CompanyAddr1</Label>
              <Col sm={10}>
                <Input type="text" CompanyAddr1="CompanyAddr1" onChange={this.handleChange.CompanyAddr1} value={this.state.CompanyAddr1} placeholder="Enter Address Line 1" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="CompanyAddr2" sm={2}>CompanyAddr2</Label>
              <Col sm={10}>
                <Input type="text" CompanyReg="CompanyAddr2" onChange={this.handleChange.CompanyAddr2} value={this.state.CompanyAddr2} placeholder="Enter Address Line 2" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="CompanyAddr3" sm={2}>CompanyAddr3</Label>
              <Col sm={10}>
                <Input type="text" CompanyAddr3="CompanyAddr3" onChange={this.handleChange.CompanyAddr3} value={this.state.CompanyAddr3} placeholder="Enter Address Line 3" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="City" sm={2}>City</Label>
              <Col sm={10}>
                <Input type="text" City="City" onChange={this.handleChange.City} value={this.state.City} placeholder="Enter City" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="State" sm={2}>State</Label>
              <Col sm={10}>
                <Input type="text" State="State" onChange={this.handleChange.State} value={this.state.State} placeholder="Enter State" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="PostCode" sm={2}>PostCode</Label>
              <Col sm={10}>
                <Input type="text" PostCode="PostCode" onChange={this.handleChange.PostCode} value={this.state.PostCode} placeholder="Enter PostCode" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="Phone" sm={2}>Phone</Label>
              <Col sm={10}>
                <Input type="text" Phone="Phone" onChange={this.handleChange.Phone} value={this.state.Phone} placeholder="Enter Phone" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="Fax" sm={2}>Fax</Label>
              <Col sm={10}>
                <Input type="text" Fax="Fax" onChange={this.handleChange.Fax} value={this.state.Fax} placeholder="Enter Fax" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="ContactPerson" sm={2}>ContactPerson</Label>
              <Col sm={10}>
                <Input type="text" ContactPerson="ContactPerson" onChange={this.handleChange.ContactPerson} value={this.state.ContactPerson} placeholder="Enter ContactPerson" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="Email" sm={2}>Email</Label>
              <Col sm={10}>
                <Input type="text" Email="Email" onChange={this.handleChange.Email} value={this.state.Email} placeholder="Enter Email" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="DealerName" sm={2}>DealerName</Label>
              <Col sm={10}>
                <Input type="text" DealerName="DealerName" onChange={this.handleChange.DealerName} value={this.state.DealerName} placeholder="Enter DealerName" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="Phone2" sm={2}>Phone2</Label>
              <Col sm={10}>
                <Input type="text" Phone2="Phone2" onChange={this.handleChange.Phone2} value={this.state.Phone2} placeholder="Enter Second Phone Number" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="Country" sm={2}>Country</Label>
              <Col sm={10}>
                <Input type="text" Country="Country" onChange={this.handleChange.Country} value={this.state.Cpuntry} placeholder="Enter Country" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="Remark" sm={2}>Remark</Label>
              <Col sm={10}>
                <Input type="text" Remark="Remark" onChange={this.handleChange.Remark} value={this.state.Remark} placeholder="Enter Remark" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="DealerCode" sm={2}>DealerCode</Label>
              <Col sm={10}>
                <Input type="text" PostCode="DealerCode" onChange={this.handleChange.DealerCode} value={this.state.DealerCode} placeholder="Enter DealerCode" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="SerialNo" sm={2}>SerialNo</Label>
              <Col sm={10}>
                <Input type="text" SerialNo="SerialNo" onChange={this.handleChange.SerialNo} value={this.state.SerialNo} placeholder="Enter SerialNo" />
              </Col>
            </FormGroup>
            <FormGroup>
              <Label for="IsActive" sm={2}>IsActive</Label>
              <Col sm={10}>
                <Input type="text" IsActive="IsActive" onChange={this.handleChange.IsActive} value={this.state.IsActive} placeholder="Enter Y or N" />
              </Col>
            </FormGroup>
          </Col>
          <Col>
            <FormGroup row>
              <Col sm={5}>
              </Col>
              <Col sm={1}>
                <button type="button" onClick={this.Addstudent} className="btn btn-success">Submit</button>
              </Col>
              <Col sm={1}>
                <Button color="danger">Cancel</Button>{' '}
              </Col>
              <Col sm={5}>
              </Col>
            </FormGroup>
          </Col>
        </Form>
      </Container>
    );
  }

}

export default AddCompany;

我试图通过存储用户键入的信息并将此信息传递给我的后端 api 来 AddCompany。似乎这是一种笨拙的方法。

有没有办法从 api 获取、存储或声明等中缩短我所有的属性写入形式?或者任何人有更好的想法来写这个?

【问题讨论】:

  • 一,你可以简单地通过 axios 发送this.state
  • 如果要发送对象中的所有内容,可以在this.state 上使用扩展运算符。
  • 嗨@ChrisG 我是 React 的新手,所以我想请你演示一下如何以你的方式做?非常感谢!
  • 与反应无关;您正在创建一个副本,但这根本不是必需的:axios.post('http://localhost:57025/api//companyedit/', this.state).then(... Axios 只是读取该数据,因此直接传递状态是非常安全的。
  • 你有CompanyName="CompanyName",应该是id="CompanyName"。也没有this.handleChange.CompanyName,您需要将其设为参数并使用柯里化。见这里:codesandbox.io/s/cool-merkle-gscvm(替换了前三个字段,每个字段一行)。

标签: javascript reactjs react-router axios


【解决方案1】:
AddCompany = () => {
axios.post('http://localhost:57025/api//companyedit/', {
   ...this.state
},
...
...

如果 Api 的请求正文具有相同的键名,这应该可以解决它。 否则,您可以为表单字段创建映射器到 Api 请求正文的字段。喜欢:

const myMapper = {
      'CompanyName': 'ApiCompanyName',
      ...
      ...
}

并运行一个循环来获取正确的请求体。

【讨论】:

  • 再一次,为什么{ ...this.state } 可以this.state?无需创建副本。
  • 是的。刚刚添加以防您想进行一些更改,因此最好创建一个参考。
  • 我的意思是:axios.post(url, this.state).then(...
【解决方案2】:

我的方法是将所有属性存储在单独的文件中。 例如。数据.js

export const attributes = {
      CompanyName: '',
      CompanyReg: '',
      CompanyAddr1: '',
      CompanyAddr2: '',
      CompanyAddr3: '',
      City: '',
      State: '',
      PostCode: '',
      Phone: '',
      Fax: '',
      ContactPerson: '',
      Email: '',
      DealerName: '',
      Phone2: '',
      Country: '',
      Remark: '',
      DealerCode: '',
      SerialNo: '',
      IsActive: '',
}

并在当前页面中导入该对象,如

import { attributes } from './your_location';

初始化状态如下:

constructor(){
 super();
 this.state = {...attributes}
}

在发布请求时像这样发送

axios.post('http://localhost:57025/api//companyedit/', {...this.state})

这会很好用,看起来很有条理。

谢谢。

【讨论】:

  • 嗨@Pritiam,这对我来说是一个非常清晰的格式。谢谢!
  • 我很确定我做了同样的事情:D
  • 这个答案没有解决对我来说最重要的问题:减少 JSX 中的大量重复代码。当然,将属性放在单独的文件中可能更有条理,但如果其他地方也不需要它们,这并没有任何实际好处。
  • @ChrisG umm... 为了减少 JSX 代码的更多冗余,我们可以创建另一个返回 的功能组件。由于 state 属性 name 和 Input,Label props 是相同的,所以我们可以通过创建循环轻松地将这些 state 属性作为 props 传递。
  • Ne 需要向 me 解释这一点,但我刚刚意识到 OP 甚至没有询问缩短所有内容。所以你的答案很好。我仍然觉得应该指出 JSX 中的极端冗余。
猜你喜欢
  • 2020-01-04
  • 2012-04-04
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 2020-01-04
  • 1970-01-01
相关资源
最近更新 更多