jose-renato-m
jose-renato-m

Reputation: 137

How to transform a Class Component to a Function Stateless Component?

I'm trying to transform a class component to function component. But it is a little bit dificult to me. Could you help me?

I've tried to do this, but it hasn´t worked:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import MovieCardsList from './MovieCardsList';

function (
  props.profiles
  props.movies
  props.users
) {
 return (
      <div>
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">ReactND - Coding Practice</h1>
        </header>
        <h1>How Popular is Your Favorite Movie?</h1>
        <MovieCardsList profiles={profiles} movies={movies} users={users} />
      </div>
    ); 
  }

export default App;

Here a part of the code:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import MovieCardsList from './MovieCardsList';

class App extends Component {
  render() {
    return (
      <div>
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">ReactND - Coding Practice</h1>
        </header>
        <h1>How Popular is Your Favorite Movie?</h1>
        <MovieCardsList profiles={profiles} movies={movies} users={users} />
      </div>
    );
  }
}

export default App;

Am I close to the solution? Any help, I appreciate. Thanks in advance.

Upvotes: 0

Views: 76

Answers (2)

Willman.Codes
Willman.Codes

Reputation: 1413

Either accept props as an argument:

function App = (props) => {
    cosnt {profiles, movies, users} = props
    return <div/>
}

or de-structure them right away

function App = ({profiles, movies, users}) => {
    return <div/>
}

Full Code:

import React from 'react';
import logo from './logo.svg';
import './App.css';
import MovieCardsList from './MovieCardsList';

function App = ({profiles, movies, users}) => {
    return (
      <div>
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">ReactND - Coding Practice</h1>
        </header>
        <h1>How Popular is Your Favorite Movie?</h1>
        <MovieCardsList profiles={profiles} movies={movies} users={users} />
      </div>
    );
}

export default App;

Upvotes: 0

Dillan Wilding
Dillan Wilding

Reputation: 1111

With minimal changes:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import MovieCardsList from './MovieCardsList';

function App(props) {
   return (
      <div>
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">ReactND - Coding Practice</h1>
        </header>
        <h1>How Popular is Your Favorite Movie?</h1>
        <MovieCardsList profiles={props.profiles} movies={props.movies} users={props.users} />
      </div>
    ); 
  }

export default App;

You could also use {...props} object spread to set them on MovieCardsList or object deconstruction ({ profiles, movies, users }) to replace (props). How I personally would do it:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import MovieCardsList from './MovieCardsList';

export default ({ profiles, movies, users }) => (
  <div>
    <header className="App-header">
      <img src={logo} className="App-logo" alt="logo" />
      <h1 className="App-title">ReactND - Coding Practice</h1>
    </header>
    <h1>How Popular is Your Favorite Movie?</h1>
    <MovieCardsList profiles={profiles} movies={movies} users={users} />
  </div>
);

Upvotes: 1

Related Questions