-1

Thanks in advance.

How do update a react input value to state on button click. I have a list of input and button listed in table row. Need to update the state array of specific item.

Code is below

    <TableCard>
      <Table dataSource={itemListArray}>
        <Column
          title="ItemName"
          align="left"
          render={item => (
                    <div>
                      <Input value={item.Name} />
                      <Icon onClick={???} />
                     </div>
          )}
        />
      </Table>
    </TableCard>
Antoni
  • 1,316
  • 2
  • 16
  • 42
Ankit Jayaprakash
  • 1,040
  • 3
  • 15
  • 31

1 Answers1

1

You should create a new component for the render which gets item. In this component you can use it own state and item

class Example extends React.Component {
      state: {
        itemListArray: [{ name: 'Name', noise: 'NA' }]
      }

      handleChange: Function = ({ index, name, value }) => {
        let newValueArray = [ ...itemListArray ]

        newValueArray[index] = {
          ...newValueArray[index],
          [name]: value,
        }

        this.setState({ itemListArray: newValueArray })
      }

      render() {
        const { itemListArray } = this.state

        return (
          <>
             <TableCard>
              <Table dataSource={itemListArray}>
                <Column
                  title="ItemName"
                  align="left"
                  render={(item, index) => (
                    <ExampleItem
                      item={item}
                      index={index}
                      onChange={this.handleChange}
                    />
                  )}
                />
              </Table>
            </TableCar
          </>
        );
      }
    }
    import React, { useState } from 'react';

    function ExampleItem({ item, index, onChange }) {
      handleClick: Function = () => {
        onChange({
          index,
          name: 'name',
          value: item.name + 'a',
        })
      }

      return (
        <>
          <Input value={item.name} />
          <Icon onClick={this.handleClick}>
            Add an A to name string
          </Icon>
        </>
      );
    }

Antoni
  • 1,316
  • 2
  • 16
  • 42