Error: Objects are not valid as a React child (found: object with keys {})
Asked Answered
C

2

6

I'm a beginner learning ts for the first time. Thank you in advance for sharing your knowledge. I am making a to-do list. I used to react to complete it. But now I am using react and typescript together to complete the code. I got an error. I don't know what the problem is. Help me, please.

Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead.

enter image description here

Click here to view the full code

What I think that the problem is this file.

// contet.tsx

import React, { createContext, useReducer, useContext, Dispatch } from 'react';
import reducer from "./reducer";
import { Action } from './actions'

export interface ITodo {
  id: string;
  text: string;
};

export interface State {
  toDos: ITodo[];
  completed: ITodo[];
}

interface ContextValue {
  state: State;
  dispatch: Dispatch<Action>;
}
export const initialState = {
  toDos: [],
  completed: [],
};

const ToDosContext = createContext<ContextValue>({
  state: initialState,
  dispatch: () => { console.error("called dispatch outside of a ToDosContext Provider") }
});

export const ToDosProvider = ({ children }: { children: React.ReactNode }) => {
  const [state, dispatch] = useReducer(reducer, initialState);
  return (
    <ToDosContext.Provider value={{ state, dispatch }}>
      {children}
    </ToDosContext.Provider>
  );
};

export const useTodosState = (): State => {
  const { state } = useContext(ToDosContext);
  return state;
};

export const useTodosDispatch = (): Dispatch<Action> => {
  const { dispatch } = useContext(ToDosContext);
  return dispatch;
};

This is App.tsx

import React from "react";
import Add from "./Add";
import Title from "./Title";
import Progress from "./Progress";
import List from "./List";
import ToDo from "./ToDo";
import styled from "styled-components";
import { useTodosState } from '../context';

const App = () => {
  const { toDos, completed } = useTodosState();
  console.log(toDos);
  return (
    <Title>
      <Add />
      <Progress />
      <Lists>
        <List title={toDos.length !== 0 ? "To Dos" : ""}>
          {toDos.map((toDo) => (
            <ToDo key={toDo.id} id={toDo.id} text={toDo.text} isCompleted={false} />
          ))}
        </List>
        <List title={completed.length !== 0 ? "Completed" : ""}>
          {completed.map((toDo) => (
            <ToDo key={toDo.id} id={toDo.id} text=
              {toDo.text} isCompleted />
          ))}
        </List>
      </Lists>
    </Title >
  )
}
export default App;
Conk answered 16/2, 2021 at 7:22 Comment(0)
B
4

I had a look at the repo you shared the problem is at List.tsx component and the way you are trying to access your props from your components. It should be

const List = ({ title, children }: any) => (

instead of

const List = (title: any, children: any) => (

as in react functional components take only one parameter the props object.

Also if you want to add types there you could do {title:string; children: ReactElement| ReactElement[]}

Benzofuran answered 16/2, 2021 at 8:45 Comment(0)
W
1

I think it has being a better way for this situation. You can use PropsWithChildren you can check it out for details.

for little example

export interface SearchProps {
  height: number
}

function Search({ children }: PropsWithChildren<SearchProps>) {
..
..
return()
}
Withrow answered 8/6, 2021 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.