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.