1

I have tried some solutions that came by, on this link particularily...

I tried changing value inside my TodosContext.js file.. which didn't work, too.. One more thing that I have tried is to call useContext() function from another component, that didn't work neither..

Here's my code. App.js:

import React, { useState, useContext } from 'react';
import TodoList from './components/TodoList';
import NewTodo from './components/NewTodo';
import { TodosProvider, TodosContext } from './components/contextapi/TodosContext';

function App() {

  const [input, setInput] = useState('');
  const [todos, setTodos] = useContext(TodosContext);

  const _handleInput = (e) => {
    setInput(e.target.value)
  }

  const _todoAdd = (e) => {
    if (e.key === 'Enter') {
      setTodos(
        [...todos, { content: input, id: Date.now(), completed: false }]
      )
      setInput('')
    }
  }

  const _todoRemove = (id) => {
    const newTodos = todos.filter(todo => todo.id !== id)
    setTodos(newTodos)
  }

  return (
    <div>
      <header>
        <h3>To-Do Manager | Context API</h3>
      </header>
      <TodosProvider>
        <NewTodo newTodo={_todoAdd} handleInput={_handleInput} newVal={input} />
        <TodoList todos={todos} />
      </TodosProvider>
    </div>
  );
}

export default App;

TodosContext.js:

import React, { useState, createContext } from 'react';

export const TodosContext = createContext()

export const TodosProvider = ({ children }) => {
    const [todos, setTodos] = useState([]);
    return (
        <TodosContext.Provider value={[todos, setTodos]}>{children}</TodosContext.Provider>
    )
}

TodoList.js:

import React, { useContext } from 'react';
import Todo from './Todo';
import RemoveTodoFromList from './RemoveTodoFromList';
import { TodosContext } from './contextapi/TodosContext'

function TodoList() {

    const [todos, setTodos] = useContext(TodosContext);

    return (
        <div>
            {todos.map(todo => (
                <div>
                    <Todo key={todo.id} todo={todo} />
                </div>
            ))}
        </div>
    )
}

export default TodoList

I'm really struggling with this one, I spent whole day figuring out what went wrong.. Thanks!

3
  • 2
    Can u try export const TodosContext = createContext([[],() => {}]). This sets a default value on creation. Commented Aug 12, 2020 at 17:12
  • @Stutje Yeah man! It works! I really appreciate it, but is it necessary to write that syntax all the time or is it just in my case? Commented Aug 12, 2020 at 18:18
  • You should always give a default value in the createContext or at least specify it so you won't stumble into those annoying errors :) glad i could help Commented Aug 13, 2020 at 7:48

1 Answer 1

5

We fixed it inside the comments.

createContext needs an object as parameter that defines your context. In your case it should be export const TodosContext = createContext([[],() => {}]). So the application knows the first element of the tuple is an array and so iterable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.