S
S
Shato Daltsaev2020-04-14 11:01:40
React
Shato Daltsaev, 2020-04-14 11:01:40

How to insert values ​​into an object field using SetState?

I'm writing an application in React, making a form and ran into a problem.

There is a model
PrePaymentParameters.ts

export interface PrePaymentParameters {
    PartnerId: number;
    OrderNumber: string;
    WebSite: string;
    AccountNumber: string;
    Sign: string;
    SumValue: number;
    EmailClient: string;
    EmailOperator: string;
    NumberTransactions: number;
    SumTransaction: number;
}

I import it into the form and throw it into the state
import * as React from "react";
import 'bootstrap/dist/css/bootstrap.min.css';
import Field from '../../components/Field/Field';
import Form from '../../components/Form/Form';
import ContentHeader from '../../components/ContentHeader/ContentHeader';
import { PrePaymentParameters } from '../../models/Orders/Operations/PrePaymentParameters'
import {
    Card,
    CardBody,
    Col,
    Row,
} from 'reactstrap';
import Axios from "axios";

export interface IFormState {
    submitSuccess?: boolean;
    prePayment: PrePaymentParameters;
}


export default class PaymentByCardPage extends React.Component<{}, IFormState> {

    constructor(props: {}) {
        super(props);

        this.state = {
            prePayment: {
                PartnerId: 766,
                OrderNumber: "",
                WebSite: "sa",
                AccountNumber: "",
                Sign: "gdfgmsdlkfdfngjm",
                SumValue: 0,
                EmailClient: "[email protected]",
                EmailOperator: "[email protected]",
                NumberTransactions: 5,
                SumTransaction: 500
            }

        };
    }

Now I'm trying to set the values ​​when the OnChange event occurs on the Input, but I can't access the field inside the object directly.
<Field id="SumValue" label="Сумма предоплаты" onChange={e => this.setState({ prePayment: {SumValue : 1000 } })}


There are two questions
1. How do I insert a value into an object field?
2. How to initialize fields with number type in a constructor? With strings, everything is clear, you can do it like this
AccountNumber: "", but with numbers I can’t understand how.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
igorkorneichuk, 2020-04-14
@Shatoidil

I would make the first question through https://github.com/kolodny/immutability-helper/ (update() method).
Second question, is your SumValue: 0 not suitable?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question