I have a MovieSearch
component:
render() {
const greeting = 'Welcome to React'
return (
<React.Fragment>
<SearchAPI />
<SearchAPIResults message={greeting}/>
</React.Fragment>
)
}
Which passes a string prop to it's child SearchAPIResults
component:
// works
function SearchAPIResults(props) {
return (
<h1>{props.message}</h1>
);
}
// does not work
// class SearchAPIResults extends React.Component {
// constructor(props) {
// super(props)
// this.state = {message: props.message}
// }
// }
The top code segment works. If I try the bottom code I get a message in my MovieSearch
component:
Type '{ message: string; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly<{}> & Readonly<{ children?: ReactNode; }>'. Property 'message' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly<{}> & Readonly<{ children?: ReactNode; }>'.
I know there's a difference between Class and Function components and I think the SearchAPIResults
component should be a Function component since it's only displaying some data. But I'm still wondering how I would pass props between 2 Class components.
this.props
? – Second Son