S
S
Semmy_992019-01-17 11:47:24
JavaScript
Semmy_99, 2019-01-17 11:47:24

How to correctly pass a parameter to a function?

Hello everyone. I use React and TypeScript
Please tell me, there are two inputs, value which are stored in the state.
There is a function in the class.

changeValue = (e: any, nameInput: string):void => {
    const value = e.target.value;
    this.setState({ [nameInput]: value })
  }

In the nameInput parameter, I pass the name of the variable that lies in the state and is responsible for storing Value
For example:
<Input
                placeholder="Name"
                value={modalName}
                onChange={(e) => { this.changeValue(e, modalName) }}
              />
              <Input
                placeholder="Description"
                value={modalDescr}
                onChange={(e) => { this.changeValue(e, modalDescr) }}

              />

The result is such an error. Tell me what is the reason for this and how to get around it?
5c40403591063848058632.png
Error text if not visible in photo
C:/src/components/Table/TableWrap.tsx
(107,19): Argument of type '{ [x: string]: any; }' is not assignable to parameter of type 'State | ((prevState: Readonly, props: Readonly) => State | Pick) | Pick<...>'.
Type '{ [x: string]: any; }' is missing the following properties from type 'Pick': name, newTRows, descr, idTable, and 3 more.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2019-01-17
@0xD34F

googled for you

S
sharkdest, 2019-01-17
@sharkdest

Try:

changeValue = (e: any, nameInput: string):void => {
    this.setState({ [nameInput]: e.target.value })
}

maybe because of a typo in the word target .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question