I'm new in to build a web with React Libray, and use next.js with typscript to create the web app. And I'm trying to build a todo list web app.
I create an interface for collet the list data like below:
interface listData {
text: string,
isDone: boolean,
viewMode?: { display: '' },
editMode?: { display: 'none' },
}
const [list, setList] = useState<listData[]>([]);
const addList = (item: any) => {
if (item !== "") {
const newList = [...list, { text: item, isDone: false, viewMode: { display: '' }, editMode: { display: 'none' } }];
localStorage.setItem('listData', JSON.stringify([...list, { text: item, isDone: false, viewMode: { display: '' }, editMode: { display: 'none' } }]));
setList(newList);
};
}
I got the error on vscode down below when I setList(newList):
Argument of type '(listData | { text: string; isDone: boolean; viewMode: { display: string; }; editMode: { display: string; }; })[]' is not assignable to parameter of type 'SetStateAction<listData[]>'.
Type '(listData | { text: string; isDone: boolean; viewMode: { display: string; }; editMode: { display: string; }; })[]' is not assignable to type 'listData[]'.
Type 'listData | { text: string; isDone: boolean; viewMode: { display: string; }; editMode: { display: string; }; }' is not assignable to type 'listData'.
Type '{ text: string; isDone: boolean; viewMode: { display: string; }; editMode: { display: string; }; }' is not assignable to type 'listData'.
The types of 'viewMode.display' are incompatible between these types.
Type 'string' is not assignable to type '""'.ts(2345)
The viewMode and editMode is build for display css to show which tag will be show, like when user press the edit button, then the viewmode.display: 'none', otherwise editMode.display: 'none'.
And now I got a problem about addList assignment problem, please look at the image. I not sure what the prolem is now in addList function when I setList(newList). Hope someone can tell me where am I worng, and really thanks for it.
return (
<>
<div className={styles.bodyOuterDiv} >
...
<ul >
{list.map((item, index) => {
return (
<li key={index} className={styles.todoListOuterLi} >
<div className={item.isDone ? styles.todoListItemsWithIsDone : styles.todoListItems} style={item.viewMode}>
{item.text}
</div>
<textarea className={styles.todoListTextArea} value={item.text} onChange={(e) => { setItem(e.target.value) }} style={item.editMode} />
...
</li>
);
})}
</ul>
...
</div>
</>
);