React: "Error T.filter Is Not A Function" Or "Error: Uncaught TypeError: T.find Is Not A Function" --> Trying To Update Object In Array
React newbie is messing things up, sorry, but really tried for hours ;( see attempts below.
Simple task: Trying to update an object in an array of objects.
This should be fairly easy though after research of a dozen answers, trying a bunch of possible solutions I still get errors. I can't figure out what I am missing here.
Here are my 4 attempts:
Attempt 1
updateText = (updatedText) => {
var arrTexts = {...this.state.arrTexts}
var myObjectToUpdate = arrTexts.filter(x => x.id === updatedText.id);
myObjectToUpdate = updatedText;
console.log (myObjectToUpdate);
console.log (arrTexts);
};
Attempt 2:
updateText = (updatedText) => {
var arrTexts = {...this.state.arrTexts}
var myObjectToUpdate = arrTexts.find(function (myObjectToUpdate) { return myObjectToUpdate.id === updatedText.id; });
myObjectToUpdate = updatedText
console.log (myObjectToUpdate);
console.log (arrTexts);
};
Attempt 3
updateText = (updatedText) => {
var arrTexts = {...this.state.arrTexts}
var myObjectToUpdate = arrTexts.findIndex(x => x.id === updatedText.id);
myObjectToUpdate = updatedText;
console.log (myObjectToUpdate);
console.log (arrTexts);
};
Attempt 4
updateText = (updatedText) => {
var arrTexts = {...this.state.arrTexts}
var myObjectToUpdate = _.findWhere(arrTexts, { id: updatedText.id });
myObjectToUpdate = updatedText;
console.log (myObjectToUpdate);
console.log (arrTexts);
};
The "updateText" comes from another component that includes an form and handles onSubmit this function:
handleUpdate = event => {
event.preventDefault();
const updatedText = {
...this.props.arrText,
id: this.idRef.current.value,
title: this.titleRef.current.value,
author: this.authorRef.current.value,
};
this.props.updateText(updatedText);
};
Thanks so much for helping out!
Answer
filter
, find
and findIndex
are all functions applicable on array. You data seems to be an array, but are cloning it to an object. You would clone it like var arrTexts = [...this.state.arrTexts]
updateText = (updatedText) => {
var arrTexts = [...this.state.arrTexts]
var myObjectToUpdate = arrTexts.find(function (myObjectToUpdate) { return myObjectToUpdate.id === updatedText.id; });
myObjectToUpdate = updatedText
console.log (myObjectToUpdate);
console.log (arrTexts);
};
Also you would update it like
handleUpdate = event => {
event.preventDefault();
const updatedText = {
id: this.idRef.current.value,
title: this.titleRef.current.value,
author: this.authorRef.current.value,
};
this.props.updateText(updatedText);
};
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM