简体   繁体   English

当 object 数组中的属性发生变化时,使用钩子对重新渲染组件做出反应

[英]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 .现在,当使用setPropertyNameuseState时,这可以正常工作。 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.

相关问题 我需要一个组件来检查状态并在另一个组件更改后重新渲染(使用 React Hooks) - I need a component to check state and rerender after another component changes (using React Hooks) 使用钩子时如何重新渲染表单值和状态值,并且在不同的组件中更改状态 - How do I rerender form value and state value when using hooks and state is changed in a different component in react 使用 setState 将空数组设置为 object 属性时出错(反应 Hooks) - Error when using setState to set an empty array to object property ( react Hooks) 当状态改变时,React 不会重新渲染我的组件 - React wont rerender my component when state changes 使用反应钩子不可变地更新对象数组中的属性 - updating a property in an array of object immutably using react hooks 当数据 class 属性更改时重新渲染 React 组件 - Make a React component rerender when data class property change 在 React 中,当用于渲染的数组发生变化时,如何避免不必要的重新渲染? - In React, how to avoid unnecessary rerender when array used for rendering changes? 当更改作为数组传递的道具时,React功能组件不会重新渲染 - React functional component doesnt rerender when changing props passed as an array 使用钩子更新反应组件中的 object 值 - Update object value in react component using hooks 如何使用反应钩子将对象数组从子组件发送到父组件并存储在父对象中? - How to send array of objects data from child component to parent component and store in the parent object using react hooks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM