I'm still new to typeScript, so here is what I'm trying to do:
I have one functional component from which I try to navigate to another one using History.push, so the pattern I try to use is something like this:
history.push({
to: "some URL",
state: {// some state}
})
so it doesn't seems to be working, so here is my parent component (the one I'm trying to navigate from)
import React from 'react';
import { VideoDataType } from '../Shared/VideoDataType/VideoDataType';
import { Options } from 'react-youtube';
import moment from 'moment';
import { useHistory, withRouter, RouteComponentProps } from 'react-router-dom';
import './VideoCard.styles.css';
interface ChildComponentProps extends RouteComponentProps<any> {
/* other props for ChildComponent */
video: VideoDataType;
opts: Options;
}
const VideoCard = ({
video,
opts,
}: ChildComponentProps): React.ReactElement => {
const history = useHistory();
const handleVideoSelect = (): void => {
history.push({
to:`/video/${video.id.videoId}`,
state: {video, opts}
});
};
return (
<div
className='videoCArd__wrapper fix_margins'
onClick={() => handleVideoSelect()}
>
<img src={video.snippet.thumbnails.medium.url} alt='avatar' />
<div className='videoCard__info'>
<div className='videoCard__text'>
<h4 className='videoCard__title'> {video.snippet.title}</h4>
<div className='videoCard__headline'>
<p> {video.snippet.channelTitle} ● </p>
<p> {moment(video.snippet.publishedAt).fromNow()} </p>
</div>
<div className='videoCard__description'>
{video.snippet.description}
</div>
</div>
</div>
</div>
);
};
export default withRouter(VideoCard);
and here is my targeted component (the now I'm navigating to)
import React from 'react';
import { Options } from 'react-youtube';
import { VideoDataType } from '../Shared/VideoDataType/VideoDataType';
import { RouteComponentProps, withRouter } from 'react-router-dom';
interface ChildComponentProps extends RouteComponentProps<any> {
/* other props for ChildComponent */
}
const SingleVideoDisplayerPage =
(): React.ReactElement<ChildComponentProps> => {
return (
<div>
<h6> jksdgfdsghjfkd </h6>
</div>
);
};
export default withRouter(SingleVideoDisplayerPage);
and here is the issue:
No overload matches this call.
Overload 1 of 2, '(path: string, state?: unknown): void', gave the following error.
Argument of type '{ to: string; state: { video: VideoDataType; opts: Options; }; }' is not assignable to parameter of type 'string'.
Overload 2 of 2, '(location: LocationDescriptor<unknown>): void', gave the following error.
Argument of type '{ to: string; state: { video: VideoDataType; opts: Options; }; }' is not assignable to parameter of type 'LocationDescriptor<unknown>'.
Object literal may only specify known properties, and 'to' does not exist in type 'LocationDescriptorObject<unknown>'. TS2769
20 | const history = useHistory();
21 | const handleVideoSelect = (): void => {
> 22 | history.push({
| ^
23 | to: `/video/${video.id.videoId}`,
24 | state: { video, opts },
25 | });