如何保留添加到收藏列表中的组件的状态

crg*_*g63 7 javascript jsx reactjs themoviedb-api react-router

我尝试在三个全局视图中保留状态.我有MovieRowPopular收藏夹和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})
}
Run Code Online (Sandbox Code Playgroud)

你有没有想过谁帮助我?

我们看到国家迷失在这张照片中.依赖于状态的按钮的颜色已被重置.

查看受欢迎的第一部电影添加到fav

用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}
Run Code Online (Sandbox Code Playgroud)

Ces*_*iva 5

我重新编写了您的原始代码,以提供一个示例,说明如何Popular在遍历路由时保留组件中的状态。

请参阅下面的链接。快乐编码。

代码沙盒链接

在此处输入图片说明