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 useState update object in array of objects

this.setState({items: this.state.items.map(x => x.id === someId ? {...x, attr:'val'} : x)});
Comment

update array useSTATE

<body>
<div id="root"></div>

<script>
const { useState } = React;

const App = () => {
    const [myArray, updateMyArray] = useState([]);

    const onClick = () => {
        updateMyArray( arr => [...arr, `${arr.length}`]);
    };
    return [
        <input type="button" onClick={ onClick } value="Update" />,

        <div>{myArray.map( e =>
          <div>{ e }</div>
        )}
        </div>
    ];
}

ReactDOM.render(
    <App />,
    document.getElementById("root")
);
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.1/umd/react-dom.production.min.js"></script>
</body>
Comment

Update an object as state with React hooks

const [state, setState] = useState({ fName: "", lName: "" });
const handleChange = e => {
    const { name, value } = e.target;
    setState(prevState => ({
        ...prevState,
        [name]: value
    }));
};

<input
    value={state.fName}
    type="text"
    onChange={handleChange}
    name="fName"
/>
<input
    value={state.lName}
    type="text"
    onChange={handleChange}
    name="lName"
/>
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

update array hooks react

new item
Comment

How do you update a value inside an array of objects with useState?

update an object within an array
Comment

PREVIOUS NEXT
Code Example
Typescript :: online ts compiler 
Typescript :: install dependencies angular 
Typescript :: filtering objects in django templates 
Typescript :: multer s3 file upload 
Typescript :: how to make dots react native 
Typescript :: 10 elements of gothic literature 
Typescript :: fwrite() expects parameter 2 to be string, array given 
Typescript :: land features created by plates moving toward each other 
Typescript :: python write a program that asks the user for a weight in kilograms and converts it to pounds 
Typescript :: The dialect mongodb+srv is not supported. Supported dialects feathers 
Cpp :: whole size of the internet 
Cpp :: add c++ 
Cpp :: c++ primality test 
Cpp :: string to wstring 
Cpp :: is javascript for websites only 
Cpp :: check gpu usage jetson nano 
Cpp :: c++ example 
Cpp :: pair in stack 
Cpp :: count a character in a string c++ 
Cpp :: sum vector c++ 
Cpp :: rapidjson write stringbuffer to file 
Cpp :: c++ converting centimeters to meters 
Cpp :: pyramid shape in c++ 
Cpp :: c++ loop programs 
Cpp :: extern __shared__ memory 
Cpp :: c++ remove whitespace from string 
Cpp :: c++ random number generator uniform distribution 
Cpp :: freopen c++ 
Cpp :: format c++ discord 
Cpp :: clear console c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =