[英]React rerender component using hooks when property in an array of object changes
So I am in a situation where I have to change a particular property from an array of objects.所以我处于必须从对象数组中更改特定属性的情况。 When the property changes I want to rerender the component.当属性更改时,我想重新渲染组件。 Now, this works fine without any issues when use the setPropertyName
of useState
.现在,当使用setPropertyName
的useState
时,这可以正常工作。 But now I am just changing one property of the object instead of the entire object.但现在我只是更改 object 的一个属性,而不是整个 object。
Here is the code that Im working on:这是我正在处理的代码:
const [movieList, setMovieList] = useState([]);
Calling the setMovieList and passing an array will obviously cause a rerender.调用 setMovieList 并传递一个数组显然会导致重新渲染。
Consider the following contents of movieList:考虑movieList的以下内容:
movieList = [
{
'name': 'Mulholland Dr.'
'year':2001,
'watched' : true,
'rating':0
},
{
'name': 'Zodiac'
'year':2007,
'watched' : false,
'rating':0
},
{
'name': 'Twin Peaks'
'year':2017,
'watched' : true,
'rating': 0
}]
Then I have a function which renders the list:然后我有一个 function 呈现列表:
function showMovieList () {
return movieList.map((movie) => {
return (
<List.Item key={movie.imdbID}>
<div className="watchedCheckBoxContainer">
<input type="checkbox" onChange={(event) => movie.watched = event.target.checked} id={`cb1${movie.imdbID}`}/>
<label htmlFor={`cb1${movie.imdbID}`}><Image size='tiny' src={movie.Poster} /></label>
</div>
{/* <Image size='tiny' src={movie.Poster} /> */}
<List.Content>{movie.Title}</List.Content>
{movie.watched ? <Rating maxRating={5} onRate={(event, {rating}) => movie.userRating=rating}/> : null}
</List.Item>
)
});
}
As you can see, when the checkbox is clicked it changes the value of the watched property.如您所见,当复选框被单击时,它会更改被监视属性的值。 A few lines later I'm checking if movie.watched == true
then show the <Rating>
component.几行之后,我检查是否movie.watched == true
然后显示<Rating>
组件。 But in this case, I'm not using setMoviesList to update the moviesList and hence the <Rating>
component is not visible.但在这种情况下,我没有使用 setMoviesList 来更新 moviesList,因此<Rating>
组件不可见。
How can I use setMoviesList
to update watched property of the particular movie whose checkbox I click on?如何使用setMoviesList
更新我单击其复选框的特定电影的已观看属性?
Okay.. I solved it by the following way:好的..我通过以下方式解决了它:
function onMovieWatched (watched, index) {
const tempMoviesList = [...movieList];
tempMoviesList[index].watched = watched;
setMovieList(tempMoviesList);
}
<input type="checkbox" onChange={(event) => onMovieWatched(event.target.checked, idx)} id={`cb1${movie.imdbID}`}/>
The idx
is the index that I am using from the map method. idx
是我在 map 方法中使用的索引。 Initially I was afraid that I might have to loop over the entire array and get the object that matches the imdbID and then update its property.最初我担心我可能不得不遍历整个数组并获取与 imdbID 匹配的 object,然后更新其属性。
Luckily I have the index while mapping over it, so I just used that to directly retrieve the object.幸运的是,我在映射它时有索引,所以我只是用它来直接检索 object。
Dont know why I didnt think of this solution before posting.不知道为什么我在发布之前没有想到这个解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.