Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Typescript, Type '' is not assignable to type 'null'

I have the start of s simple todo app in with React and typescript

I am creating a context but getting an error in the value of the content provider

<TodoContext.Provider value={contextValue}>{children}</TodoContext.Provider>

for the value I get the error

Type '{ todoList: string[]; }' is not assignable to type 'null'.

What does error mean and how can i fix this typescript error

import { createContext, useContext, useState, ReactChildren, ReactChild } from "react";

interface AuxProps {
    children: ReactChild | ReactChildren;
  }

const TodoContext = createContext(null)

const intialTodo = ['Learn Context']

const TodoProvider = ({children}:AuxProps) => {

    const [todoList, setTodoList] = useState<string[]>(intialTodo)

    const contextValue = {
        todoList
    }

    return(
        <TodoContext.Provider value={contextValue}>{children}</TodoContext.Provider>
    )
}

export const useTodoContext = () => useContext(TodoContext)

export default TodoProvider
like image 446
lomine Avatar asked Oct 15 '25 03:10

lomine


2 Answers

Because you are sending just null. but context needs null with type here like that

const TodoContext = createContext(null as any)
render{
 const photosObject = this.context as any
}
like image 155
Foxsnow Avatar answered Oct 18 '25 05:10

Foxsnow


Why are you assigning null to string array?

const TodoContext = createContext([])

does it work for your case?

like image 22
Ozan Mudul Avatar answered Oct 18 '25 05:10

Ozan Mudul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!