如何保留添加到收藏夹列表的组件的状态
How to preserve state for component added to an favourite list
我尝试在三个全局视图中保留状态。
我有组件 MovieRow
,它在 Popular
、收藏夹和 Search
组件中被调用。目标是组件 MovieRow
在此视图中调用时保留其状态 (Popular
...)
例如,如果我有 2 部电影,我检查了其中一部电影以添加到收藏夹,这部电影应该保持状态。现在,如果我在单击并更改全局视图时将电影添加到收藏列表,组件 MovieRow
将再次挂载并重置状态。
我尝试了很多不同的方法。将带有条件渲染的状态一个全局变量存储到我的 MovieRow
和其他
编辑:您可以在此处查看完整代码https://codesandbox.io/s/lpjp0oxmmq
编辑 2:我成功存储了我的状态,但无法正确使用。
componentWillUnmount () {
let storeState = localStorage.setItem('someSavedState', JSON.stringify(this.state))
console.log(" ------------------- unmount ------------------- ")
console.log(JSON.parse(localStorage.getItem('someSavedState')))
console.log(" ------------------- ------- ------------------- ")
}
componentWillMount() {
const rehydrate = JSON.parse(localStorage.getItem('someSavedState'))
console.log(" ------------------- will mount ------------------- ")
console.log(rehydrate)
console.log(" ------------------- ---- ----- ------------------- ")
// this.setState({isFaved: rehydrate})
}
你知道谁帮了我吗?
我们看到状态在这张照片中迷失了方向。重置依赖于状态的按钮颜色。
View popular with the first movie added to fav
MoviesRow.js :
import React from 'react'
import '../css/MovieRow.css';
import { APIKEY, baseURL } from '../../App'
import { filter } from '../../View/Popular'
var myFavoriteMovies = []
function IsFav(props) {
return (
<div movieId={props.movie.id} className="MovieRow">
<div>
<img alt="poster" src={props.posterSrc} />
</div>
<div>
<h3>{props.movie.title}</h3>
<p>{props.movie.overview}</p>
<input type="button" value="View"/>
<button onClick={props.onClick} className=" heart">
</button>
</div>
</div>
);
}
function IsNotFav(props) {
return (
<div movieId={props.movie.id} className="MovieRow">
<div>
<img alt="poster" src={props.posterSrc} />
</div>
<div>
<h3>{props.movie.title}</h3>
<p>{props.movie.overview}</p>
<input type="button" value="View"/>
<button onClick={props.onClick} className="toggled heart">
</button>
</div>
</div>
);
}
class MovieRow extends React.Component {
constructor(props) {
super(props)
this.addFavorite = this.addFavorite.bind(this)
this.deleteFavorite = this.deleteFavorite.bind(this)
this.state = {
isFaved: false,
}
}
viewMovie() {
const url = "https://www.themoviedb.org/movie/" + this.props.movie.id
window.location.href = url
console.log(this.props.movie.id)
}
addFavorite() {
this.setState({isFaved: true})
const favMovie = "".concat(baseURL, 'movie/', this.props.movie.id ,'?api_key=', APIKEY)
myFavoriteMovies.push(favMovie)
}
deleteFavorite() {
this.setState({isFaved: false})
}
render() {
const isFaved = this.state.isFaved;
let movie;
if (isFaved) {
movie = <IsNotFav movie={this.props.movie} posterSrc={this.props.posterSrc} onClick={this.deleteFavorite}/>
} else {
movie = <IsFav movie={this.props.movie} posterSrc={this.props.posterSrc} onClick={this.addFavorite}/>
}
return movie
}
}
export { MovieRow as default, myFavoriteMovies}
我修改了您的原始代码以提供一个示例,说明如何在遍历路线时在您的 Popular
组件中保留状态。
请看下面的link。编码愉快。
我尝试在三个全局视图中保留状态。
我有组件 MovieRow
,它在 Popular
、收藏夹和 Search
组件中被调用。目标是组件 MovieRow
在此视图中调用时保留其状态 (Popular
...)
例如,如果我有 2 部电影,我检查了其中一部电影以添加到收藏夹,这部电影应该保持状态。现在,如果我在单击并更改全局视图时将电影添加到收藏列表,组件 MovieRow
将再次挂载并重置状态。
我尝试了很多不同的方法。将带有条件渲染的状态一个全局变量存储到我的 MovieRow
和其他
编辑:您可以在此处查看完整代码https://codesandbox.io/s/lpjp0oxmmq
编辑 2:我成功存储了我的状态,但无法正确使用。
componentWillUnmount () {
let storeState = localStorage.setItem('someSavedState', JSON.stringify(this.state))
console.log(" ------------------- unmount ------------------- ")
console.log(JSON.parse(localStorage.getItem('someSavedState')))
console.log(" ------------------- ------- ------------------- ")
}
componentWillMount() {
const rehydrate = JSON.parse(localStorage.getItem('someSavedState'))
console.log(" ------------------- will mount ------------------- ")
console.log(rehydrate)
console.log(" ------------------- ---- ----- ------------------- ")
// this.setState({isFaved: rehydrate})
}
你知道谁帮了我吗?
我们看到状态在这张照片中迷失了方向。重置依赖于状态的按钮颜色。
View popular with the first movie added to fav
MoviesRow.js :
import React from 'react'
import '../css/MovieRow.css';
import { APIKEY, baseURL } from '../../App'
import { filter } from '../../View/Popular'
var myFavoriteMovies = []
function IsFav(props) {
return (
<div movieId={props.movie.id} className="MovieRow">
<div>
<img alt="poster" src={props.posterSrc} />
</div>
<div>
<h3>{props.movie.title}</h3>
<p>{props.movie.overview}</p>
<input type="button" value="View"/>
<button onClick={props.onClick} className=" heart">
</button>
</div>
</div>
);
}
function IsNotFav(props) {
return (
<div movieId={props.movie.id} className="MovieRow">
<div>
<img alt="poster" src={props.posterSrc} />
</div>
<div>
<h3>{props.movie.title}</h3>
<p>{props.movie.overview}</p>
<input type="button" value="View"/>
<button onClick={props.onClick} className="toggled heart">
</button>
</div>
</div>
);
}
class MovieRow extends React.Component {
constructor(props) {
super(props)
this.addFavorite = this.addFavorite.bind(this)
this.deleteFavorite = this.deleteFavorite.bind(this)
this.state = {
isFaved: false,
}
}
viewMovie() {
const url = "https://www.themoviedb.org/movie/" + this.props.movie.id
window.location.href = url
console.log(this.props.movie.id)
}
addFavorite() {
this.setState({isFaved: true})
const favMovie = "".concat(baseURL, 'movie/', this.props.movie.id ,'?api_key=', APIKEY)
myFavoriteMovies.push(favMovie)
}
deleteFavorite() {
this.setState({isFaved: false})
}
render() {
const isFaved = this.state.isFaved;
let movie;
if (isFaved) {
movie = <IsNotFav movie={this.props.movie} posterSrc={this.props.posterSrc} onClick={this.deleteFavorite}/>
} else {
movie = <IsFav movie={this.props.movie} posterSrc={this.props.posterSrc} onClick={this.addFavorite}/>
}
return movie
}
}
export { MovieRow as default, myFavoriteMovies}
我修改了您的原始代码以提供一个示例,说明如何在遍历路线时在您的 Popular
组件中保留状态。
请看下面的link。编码愉快。