I am trying to implement Typescript and Context API together in an application. I am facing the issue of implementing the function of deleteMovie and addMovie into the MovieContext.Provider's value prop.
Here's the error that I'm receiving:
Type '{ movies: MovieAttribute[]; deleteMovie: (id: number) => void; addMovie: (id: number, title: string) => void; }' is not assignable to type '{ movies: { id: number; title: string; }[]; }'.
Object literal may only specify known properties, and 'deleteMovie' does not exist in type '{ movies: { id: number; title: string; }[]; }'.ts(2322)
index.d.ts(337, 9): The expected type comes from property 'value' which is declared here on type 'IntrinsicAttributes & ProviderProps<{ movies: { id: number; title: string; }[]; }>'
From what I understand from the error, am I correct to say that I have yet to declare that my Provider's value did not include a 'function type'. If so, how can I amend this issue?
MovieContext.tsx
import React, { createContext, useReducer } from 'react';
import MovieReducer from '../reducers/MovieReducer';
const initialState = {
movies: [
{
id: 1,
title: "King Kong"
},
{
id: 2,
title: "Spiderman"
}
]
};
export const MovieContext = createContext(initialState);
export const MovieProvider = (props: { children: React.ReactNode }) => {
const [state, dispatch] = useReducer(MovieReducer, initialState);
// Actions
function deleteMovie(id: number): void {
dispatch({
type: 'DELETE_MOVIE',
payload: { id, title: 'nil' }
})
}
function addMovie(id: number, title: string): void {
dispatch({
type: 'ADD_MOVIE',
payload: { id, title }
})
}
return (
<MovieContext.Provider value={{
movies: state.movies,
deleteMovie,
addMovie
}}>
{props.children}
</MovieContext.Provider>
)
}
Do let me know if any part of the code can be improve as well! I just started working on Typescript recently and Context just literally today.