2

I'm beginning with ReactJS, and I decided to make my project using typescript. It's an app that fetches movies from an API and displays it.

Here's the app:

import React from 'react';
import '../../assets/css/App.css';
import { Thumb } from '../../components/Thumb';


interface State {
  isLoaded: boolean;
  items: any[];
  error: string;
}
class App extends React.Component<{}, State> {

  state: Readonly<State> = {
    isLoaded: false,
    items: [],
    error: ''
  }

  renderThumbsContainer() {

    const movies = [
      {title: 'Terminator', original_title: 'Terminator', original_language: 'EN', overview: 'A killer robot from future blablabla...', poster_path:'/terminator.jpg'}
    ];

    const thumbs = movies.map((movie, index) => {
      return (
        <Thumb 
          key={index}
          movie={movie}
        />
      );
    });
    return (<div>{thumbs}</div>)
  }

  render() {

    return (
      <div className="App">
        <header className="App-header">
          <h1>Movies</h1>
        </header>
        <main>
          {this.renderThumbsContainer()}
        </main>
      </div>
    );
  }

}

export default App;

And the component displaying the movie details:

import React from 'react';

export function Thumb({ movie: props }: any) {
    return (
        <div className="thumb">
            <div>{props.title}</div>
            <div>{props.original_title}</div>
            <div>{props.original_language}</div>
            <div>{props.overview}</div>
            <div>{props.poster_path}</div>
        </div>
    );
}

After spending 2 hours fighting with the issue of passing an object through props, I finally figured it out using this parameter in the Thumb component function

Thumb({ movie: props }: any)

I thought that the props parameter so Thumb(props: any) should be enough, but datas where not displayed and I finally came up with this, but I just don't understand how this works. First I displayed movie. in the divs, still didn't work and then it worked using props.

I've a lack of knowledge in typescript, for me I declared an object of type any containing a movie attribute of type props as a parameter but it makes no sense.

So I'd like to understand this, did I bind the movie attribute coming from the parent to props or something? Please explain.

1 Answer 1

5

You can see the JSX <Thumb key={index} movie={movie}/> as syntactic sugar for the function call Thumb(props) where props={key: index, movie: movie}. All your props provided in the JSX element get represented as keys and values on the props object passed to your component.

When you then do function Thumb({ movie: props }), you're destructuring the props argument, essentially taking only the movie key from the now-destructured propsargument and renaming it to "props".

I think you could try something like this instead

import React from 'react';

interface ThumbProps {
  key: number;
  movie: {
    title: string;
    original_title: string;
    // etc...
    }
}

export function Thumb({ movie }: ThumbProps) {
    return (
        <div className="thumb">
            <div>{movie.title}</div>
            <div>{movie.original_title}</div>
            <div>{movie.original_language}</div>
            <div>{movie.overview}</div>
            <div>{movie.poster_path}</div>
        </div>
    );
}

Note: function Thumb({ movie }: ThumbProps) now means that Typescript will expect the destructured propery movie to be of the type defined by the interface ThumbProps`.

Sign up to request clarification or add additional context in comments.

2 Comments

I was writing this but gustav already told, i recommend using I before interface names like IThumbProps, but that is for the coder`s choice.
I see, finally it’s been like declaring the interface in the function signature. Using the name props confused me. I was intending to declare an interface later, just that not being able to pass this object drove me crazy. Thanks for your explanation.

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.