1

I'm new to react hooks, and I'm having a hard time to convert this class, into react hooks with ES6 syntax. could somone please help me. here is my coude...

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';

import { fetchMovie, setLoading } from '../../actions/searchActions';

import Spinner from '../layout/Spinner';

export class Movie extends Component {
  componentDidMount() {
    this.props.fetchMovie(this.props.match.params.id);
    this.props.setLoading();
  }
  render() {
    const { loading, movie } = this.props;

    let movieInfo = (
      <div className="container">
       jsx......
 </div>
    );

    let content = loading ? <Spinner /> : movieInfo;
    return <div>{content}</div>;
  }
}

const mapStateToProps = state => ({
  loading: state.movies.loading,
  movie: state.movies.movie
});

export default connect(
  mapStateToProps,
  { fetchMovie, setLoading }
)(Movie);

2 Answers 2

3

Some of the most commonly used hooks are useState, useEffect. useState is similar to setState in certain ways, with one of the differences being that you don't have the callback functionality with useState. You could use a useEffect to achieve something similar. useEffect can work as componentDidMount, componentDidUpdate and componentWillUnmount. If you pass an empty set of dependancies, [](like in the following example), it behaves as componentDidMount. If you want a particular functionality to be triggered based on certain conditions such as a state being updated etc, you can pass that as a dependancy. Then, the useEffect behaves as componentDidUpdate. useEffect accepts a third argument, as a callback function. This is triggered when the component is being unmounted, thereby adding the componentWillUnmount behaviour.

import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';

import { fetchMovie, setLoading } from '../../actions/searchActions';

import Spinner from '../layout/Spinner';

const Movie = () => {
  useEffect(() => {
    props.fetchMovie(props.match.params.id);
    props.setLoading();
  }, [])
  const { loading, movie } = props;

    let movieInfo = (
      <div className="container">
       jsx......
      </div>
    );

  let content = loading ? <Spinner /> : movieInfo;
  return <div>{content}</div>;
}

const mapStateToProps = state => ({
  loading: state.movies.loading,
  movie: state.movies.movie
});

export default connect(
  mapStateToProps,
  { fetchMovie, setLoading }
)(Movie);
Sign up to request clarification or add additional context in comments.

2 Comments

You forgot to remove the this.props and to pass props as the parameter.
@RodrigoAmaral: Every time :/
1

You can rewrite to hooks like that.

import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Link } from 'react-router-dom';

import { fetchMovie, setLoading } from '../../actions/searchActions';

import Spinner from '../layout/Spinner';

export const Movie = ({ match}) => {
  const dispatch = useDispatch();
  const movie = useSelector(state => state.movies.movie);
  const loading = useSelector(state => state.movies.loading);
  useEffect(() => {
        dispatch(fetchMovie(match.params.id));
        dispatch(setLoading());
     }, []);

  return (
    <div>
      {
        loading 
         ? <Spinner /> 
         : <div className="container">
            jsx......
          </div>
      }
    </div>
  )
}

useEffect with empty array as params works like componentDidMount. And i changed connect to useSelector and useDispatch

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.