admin管理员组文章数量:1367429
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" es from another ponent 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!
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" es from another ponent 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!
Share Improve this question asked Jul 5, 2018 at 11:22 YvonCYvonC 3492 gold badges6 silver badges23 bronze badges2 Answers
Reset to default 6filter
, 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);
};
Okay so like 4 solutions:
It could just be the data hasn't loaded before the filter or find function is called. So a few troubleshooting things that I always try are:
Just use try{} catch{} around your statement block to avoid this error. Try Catch makes it so the page doesn't crash, and the data a lot of times, just loads a second later. This is how you know you're skilled at JavaScript.
Use conditional rendering and don't attempt method until condition is true. For example: if(data != null || undefined){ "run filter function crashing"}
Also really important, don't forget if you're below the return part and have to do it in JSX then just do conditional JSX instead of an if statement, like {data != null ? data : null}
Use Async Await. Make the method not run until it awaits the data. and make the task run concurrently. Let the Synchronous code reader keep running the code below until the method needing data, finally has the data it needs to run.
Okay so this is not remended. But just for your sanity, incase the 3 things I've said before are still not working. Don't forget that you can always just define the initial array object yourself. Let's say you want to map a blank array object [{}]. Just define the object towards the top, and you can even make it look like an initial loading thing, if you do like const object = [{id: "...", email: "..."}]. Something must map in this case, and the wanted data could load later.
本文标签:
版权声明:本文标题:javascript - React: "Error t.filter is not a function" or "Error: Uncaught TypeError: t.find is not a 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743721020a2527569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论