Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

react setstate in hooks to array of objects value

// sample datas structure
/* const datas = [
    {
      id:   1,
      name: 'john',
      gender: 'm'
    }
    {
      id:   2,
      name: 'mary',
      gender: 'f'
    }
] */ // make sure to set the default value in the useState call (I already fixed it)

const [datas, setDatas] = useState([
    {
      id:   1,
      name: 'john',
      gender: 'm'
    }
    {
      id:   2,
      name: 'mary',
      gender: 'f'
    }
]);

const updateFieldChanged = index => e => {

    console.log('index: ' + index);
    console.log('property name: '+ e.target.name);
    let newArr = [...datas]; // copying the old datas array
    newArr[index] = e.target.value; // replace e.target.value with whatever you want to change it to

    setDatas(newArr); // ??
}

return (
    <React.Fragment>
        { datas.map( (data, index) => {
              <li key={data.name}>
                <input type="text" name="name" value={data.name} onChange={updateFieldChanged(index)}  />
              </li>
          })
        }
    </React.Fragment>
)
Comment

React update state array of objects hooks

React fin and update state value
Comment

React update state array of objects hooks

React find and update state value
Comment

PREVIOUS NEXT
Code Example
Typescript :: convert string to bits c# 
Typescript :: calculate distance between two latitude longitude points in google maps api 
Typescript :: extend type typescript 
Typescript :: typescript array of mixed types 
Typescript :: deno current directory 
Typescript :: how to delete unused imports intellij webstorm 
Typescript :: generic arrow function typescript 
Typescript :: typescript promise 
Typescript :: distance between two points latitude longitude c# 
Typescript :: Check if value exisits update or insert sQL 
Typescript :: class-transformer default value 
Typescript :: roots of grass 
Typescript :: swal fire 
Typescript :: typescript hashmap 
Typescript :: jquery select multiple elements with same class 
Typescript :: roblox how to weld parts together using script 
Typescript :: --skip tests generate components - Angular 12 - skip-tests vs spec-false 
Typescript :: typescript how to create an array instance 
Typescript :: Create Hash Node TypeScript 
Typescript :: object add property typescript 
Typescript :: typescript check if object is of type 
Typescript :: get one property from list of objects linq 
Typescript :: Angular import from local library 
Typescript :: typeorm delete date column 
Typescript :: dota 2 space to center hero 
Typescript :: merge to datasets in r 
Typescript :: ionic 5 check if string can be a number and then make a number 
Typescript :: react native 3 dots icon 
Typescript :: jsdoc to typescript 
Typescript :: typeorm relationId 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =