0

I read and tried so hard, but still could not find a way to successfully replace componentWillReceiveProps with getDerivedStateFromProps. Here is my code, componentWillReceiveProps one is working fine. but the getDerivedStateFromProps which I tried is not working(after componentDidMount() running OK, state.movies eventually become [] empty). Thanks for your kind help!

class OK_MovieListContainer extends React.Component {


    constructor(props) {
        super(props);
        this.state = {
            movies: [],
            title:''
        };
        this.discoverMovieDB = this.discoverMovieDB.bind(this);
        this.searchMovieDBAPI = this.searchMovieDBAPI.bind(this);

    }

    componentDidMount() {
        this.discoverMovieDB();

    }


    UNSAFE_componentWillReceiveProps(nextProps, prevState) {

        if (nextProps.match.params.title && prevState.title !== nextProps.match.params.title) {

            this.searchMovieDBAPI(nextProps.match.params.title);
            this.setState({ title: nextProps.match.params.title });

        }
    }



    async discoverMovieDB() {

      .....
    }



     async  searchMovieDBAPI(title) {
        const promisedData = await MovieDBAPI.search(title);

        if (promisedData) {
                this.setState({ movies: promisedData[0] }); 
            }

        }


    }


    render() {

        return (       
            <div className="App">
                <MovieList movies={this.state.movies} />
            </div>

        );
    }
}

export default OK_MovieListContainer;

the following is what I tried with getDerivedStateFromProps , but not working(after componentDidMount() running OK, state.movies eventually become [] empty). ....T.T

class MovieListContainer extends React.Component {



    constructor(props) {
        super(props);
        this.state = {
            movies: [],
            title:''
        };
        this.discoverMovieDB = this.discoverMovieDB.bind(this);
        this.searchMovieDBAPI = this.searchMovieDBAPI.bind(this);

    }



     static getDerivedStateFromProps(nextProps, prevState) {

         if (nextProps.match.params.title && nextProps.match.params.title !== prevState.title) {

             return {
                 //this state changes then componentDidUpdate is called
                 title: nextProps.match.params.title
             };
         }

         //indicate state update not required
         return null;       
    }

    componentDidMount() {
        this.discoverMovieDB();

    }


    componentDidUpdate(nextProps, prevState) {

        if (nextProps.match.params.title !== prevState.title) {

            this.searchMovieDBAPI(nextProps.match.params.title);
            this.setState({ title: nextProps.match.params.title });
        }

    }


    async discoverMovieDB() {

       .....
    }


     async  searchMovieDBAPI(title) {
        const promisedData = await MovieDBAPI.search(title);

        if (promisedData) {

                this.setState({ movies: promisedData[0] });
            }

        }


    }


    render() {

        return (


            <div className="App">
                <MovieList movies={this.state.movies} />
            </div>

        );
    }
}

export default MovieListContainer;

Thank you so much!

fairy
  • 9
  • 2
  • what do you mean by not working? your entire state becomes empty except the title? – Sujit.Warrier Jan 13 '20 at 02:19
  • 1
    also think of using purecomponents – Sujit.Warrier Jan 13 '20 at 02:20
  • 1
    Have you tried `componentDidUpdate(prevProps, prevState)`? Doesn't seem your use-case aligns with that of [getDrerivedStateFromProps](https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops). Also, please be more specific when describing your issue than "it isn't working". – Drew Reese Jan 13 '20 at 05:56

0 Answers0