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 :: engineering adding requirements to password 
Typescript :: laravel validation exists match with nother column 
Typescript :: if a directive called 10000 times screen getting struck 
Typescript :: Highcharts error #17: www.highcharts.com/errors/17/?missingModuleFor=candlestick - missingModuleFor: candlestick 
Typescript :: About half of divorced parents try to avoid each other after the divorce, creating a different set of rules for children to follow in each parent’s household. This type of parental interaction is called 
Typescript :: HOW TO DROP ALL TABLES WITH THEIR CONSTRAINTS AT ONCE IN ORACLE 
Typescript :: number of elements in circular queue 
Typescript :: Python program to extract characters from various text files and puts them into a list 
Typescript :: fs readFile binary 
Typescript :: ts repeat string 
Typescript :: Can we nested try statements in java 
Typescript :: typescript cast to type remove properties 
Typescript :: java objects cannot change? 
Typescript :: number validation in typescript 
Typescript :: extracts lists from list python 
Typescript :: which of the foolowing ia an element of pallette that holds multiple elements of nspecific purpose 
Typescript :: typescript enum value to enum 
Typescript :: get distance beetwen two points roblox 
Typescript :: angular input change event datatype typescript 
Typescript :: tss from gene granges 
Cpp :: find largest number in vector c++ 
Cpp :: #include<bits/stdc++.h 
Cpp :: vector erase not working c++ 
Cpp :: jupyter lab use conda environment 
Cpp :: c++ SDL2 window 
Cpp :: priority queue ordered by second element 
Cpp :: angle to vector2 
Cpp :: error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/ 
Cpp :: unknown type name pid_t 
Cpp :: modf() c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =