In my simple typescript/react app, when I'm trying to map an array of objects, I'm getting an error that property 'title' does not exist on type 'object'.
const Todo = () => {
const [todos, setTodos] = useState<object[]>([])
const [title, setTitle] = useState<string>('')
const [body, setBody] = useState<string>('')
const onSubmit = (e: React.FormEvent<HTMLFormElement>): void => {
e.preventDefault()
const todo = {
title: title,
body: body
}
setTodos([todo, ...todos])
setTitle('')
setBody('')
}
return (
<>
<form onSubmit={onSubmit}>
<input value={title} onChange={e => setTitle(e.target.value)} />
<input value={body} onChange={e => setBody(e.target.value)} />
<button>submit</button>
</form>
todos:
{todos.map((todo) => <div><h3>{todo.title}</h3>
<p>{todo.body}</p></div>)}
</>
);
}
const [todos, setTodos] = useState<any[]>([])