【问题标题】:React Recoil "Argument of type 'any[]' is not assignable to parameter of type 'never[]"?React Recoil“'any []'类型的参数不能分配给'never []类型的参数”?
【发布时间】:2022-02-16 18:20:46
【问题描述】:

我正在关注React Recoil Todo List tutorial,但由于某种原因,在遵循教程时它们是类型错误,我不确定如何正确满足它们。

代码如下:

export const todoListAtom = atom({
  key: 'todoListAtom',
  default: [],
});

export function TodoItem({item}: {item: TodoItem}) {
  const [todoList, setTodoList] = useRecoilState(todoListAtom);
  const index = todoList.findIndex((listItem) => listItem === item);

  const editItemText = ({target: {value}}) => {
    const newList = replaceItemAtIndex(todoList, index, {
      ...item,
      text: value,
    });

    setTodoList(newList);
  };

setTodoList 的类型是:

const setTodoList: (valOrUpdater: never[] | ((currVal: never[]) => never[])) => void

【问题讨论】:

    标签: reactjs next.js recoiljs


    【解决方案1】:

    我认为这就是您要寻找的。你的一些代码丢失了,所以我为丢失的部分创建了替代品:

    TS Playground

    import {atom, useRecoilState} from 'recoil';
    
    type TodoItem = {
      text: string;
    };
    
    function replaceItemAtIndex <T>(array: T[], index: number, newValue: T): T[] {
      return array.map((value, i) => i === index ? newValue : value);
    }
    
    export const todoListAtom = atom<TodoItem[]>({
      key: 'todoListAtom',
      default: [],
    });
    
    export function TodoItem({item}: {item: TodoItem}) {
      const [todoList, setTodoList] = useRecoilState(todoListAtom);
      const index = todoList.findIndex((listItem) => listItem === item);
    
      const editItemText = ({target: {value}}: {target: {value: string}}) => {
        const newList = replaceItemAtIndex(todoList, index, {
          ...item,
          text: value,
        });
    
        setTodoList(newList);
      };
    
      // ...
    }
    
    

    【讨论】:

      猜你喜欢
      • 2021-06-09
      • 2021-10-23
      • 1970-01-01
      • 2018-11-03
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多