1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【React】オブジェクトにkey名と同じ変数をセットしたらエラーが出た

Posted at

Youtubeを参考にTypeScriptを使ってReactのTodoアプリを作成していたところ、エラーExpected property shorthand object-shorthandに遭遇した。
エラー:

原因はhandleSubmitnewTodoinputValue: inputValue,としているのがいけなかった模様。変数名がキー名と同じである場合、キー名を記入してはいけないらしい。

なおコードは以下の通り。

App.tsx
const [inputValue, setInputValue] = useState('');

...

interface Todo {
  inputValue: string;
  id: number;
  completed: boolean;
}
  
...

const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
  e.preventDefault();
  const newTodo: Todo = {
    inputValue: inputValue,
    id: inputValue.length,
    completed: false,
  };
  setTodos([newTodo, ...todos]);
  setInputValue('');
};

参考:エラー「Expected property shorthand object-shorthand」の対処方法

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?