提问人:krissy lynn 提问时间:9/6/2023 最后编辑:krissy lynn 更新时间:9/6/2023 访问量:15
状态对象值不会更新
state object values won't update
问:
我尝试使用 react 创建电影名称的下拉菜单。 每当下拉菜单选择发生更改时,都应通过将值与“movies”数组状态中的对象 ID 进行比较来更新“currentMovie”状态的值
这是我的代码:
import { useState } from 'react';
import MovieComp from './Movie';
function ParentComp(props) {
const [movies, setMovies] = useState( [ { id : 1 , name : "Under the Dome", pic :
"https://static.tvmaze.com/uploads/images/medium_portrait/81/202627.jpg" },
{ id : 2 , name : "Person of Interest", pic :
"https://static.tvmaze.com/uploads/images/medium_portrait/163/407679.jpg" },
{ id : 3 , name : "Bitten", pic :
"https://static.tvmaze.com/uploads/images/medium_portrait/0/15.jpg" } ] )
const [currentMovie, setCurrentMovie] = useState( {id : "" , name : "", pic : "" } )
function createDropMenu() {
return movies.map( item => {
return <option key={item.id} value={item.id} > {item.name} </option>
} )
}
function updateMovie(e) {
if (e.target.value === 0) {
console.log("test")
let selectedMovie = {id : "" , name : "", pic : "" };
let selectedMovieEntries = Object.entries(selectedMovie);
selectedMovieEntries.map( entry => {
setCurrentMovie( prev => ( {...prev, [ entry[0] ]: entry[1] } ) );
})
} else {
console.log("test")
let selectedMovie = movies.find( item => item.id === e.target.value );
let selectedMovieCopy = {};
selectedMovieCopy = {...selectedMovie};
let selectedMovieEntries = Object.entries(selectedMovieCopy);
selectedMovieEntries.map( entry => {
setCurrentMovie( prev => ( {...prev, [ entry[0] ]: entry[1] } ) );
})
}
}
return (
<div >
<h3> Pick a movie: </h3>
<select onChange={updateMovie}>
<option value={0} ></option>
{createDropMenu()}
</select> <br/>
<MovieComp name = {currentMovie.name} pic = {currentMovie.pic} />
</div>
);
}
export default ParentComp;
由于某种原因,在更改选择时,currentMovie 状态不会更改其值 我错过了什么?
答:
0赞
David
9/6/2023
#1
这将生成一个空数组:
let selectedMovieEntries = Object.entries(selectedMovie);
因此,从不遍历数组,也从不调用。.map()
setCurrentMovie
退后一步...... 似乎被严重过度设计了。如果您只需要知道“当前选择”的是哪部“电影”,那么这只不过是该“电影”记录。例如:updateMovie
id
// Initial state is undefined, since nothing is selected
const [currentMovie, setCurrentMovie] = useState()
// later...
// Then in the JSX just update state with the selected value
<select onChange={e => setCurrentMovie(e.target.value)}>
(注意:这是未经测试的,您可能需要在此处转换为数值,也可以转换为字符串。e.target.value
在这一点上,你根本不需要该函数的全部内容。然后,每当您需要获取当前选定的电影时,只需从该列表中获取它即可。例如:updateMovie
<MovieComp movie={movies.find(m => m.id === currentMovie)} />
或者,如果该组件出于某种原因确实需要两个单独的道具,请在渲染时获取当前影片:
const selectedMovie = movies.find(m => m.id === currentMovie);
并在标记中使用它:
<MovieComp name={currentMovie?.name} pic={currentMovie?.pic} />
这样做的好处是,您不再在状态中复制数据。“当前电影”只是对数据的引用,而不是数据的副本。
评论
setCurrentMovie(selectedMovie)