Type void is not assignable to WriteableDraft.

huangapple go评论75阅读模式
英文:

Type void is not assignable to WriteableDraft

问题

Here's the translated code snippet:

接下来是已翻译的代码部分:

interface ReminderState {
  reminders: Reminder[];
}

const initialState: ReminderState = {
  reminders: [],
};

export const fetchReminders = createAsyncThunk("getReminders", async () => {
  await axios.get("http://localhost:3000/api/reminders")
  .then((response) => {
    return response.data.reminders;
  })
  .catch((err: Error | AxiosError) => {
    if (axios.isAxiosError(err)) {
      return err.response
    } else {
      return err;
    }
  })
});

export const reminderSlice = createSlice({
  name: "reminder",
  initialState,
  reducers: {
    setReminders: (state, action: PayloadAction<Reminder[]>) => {
      state.reminders = action.payload;
    },
    deleteReminder: (state, action: PayloadAction<string>) => {
      state.reminders = state.reminders.filter(
        (reminder) => reminder.id === action.payload
      );
    },
  },
  extraReducers: (builder) => {
    builder
    .addCase(fetchReminders.fulfilled, (state, action) => {
      state.reminders = action.payload;
    })
  }
});

export const { setReminders, deleteReminder } = reminderSlice.actions;

export default reminderSlice.reducer;

Please note that the translated code retains the original variable and function names in English for clarity and compatibility with programming languages.

英文:

Does anyone know why I'm getting the following error?

Type &#39;void&#39; is not assignable to type &#39;WritableDraft&lt;Reminder&gt;[]&#39;.

I'm trying to create an async thunk to fetch data and store in state on app startup and I'm not sure why I'm having this in my extraReducers:

interface ReminderState {
  reminders: Reminder[];
}

const initialState: ReminderState = {
  reminders: [],
};

export const fetchReminders = createAsyncThunk(&quot;getReminders&quot;, async () =&gt; {
  await axios.get(&quot;http://localhost:3000/api/reminders&quot;)
  .then((response) =&gt; {
    return response.data.reminders;
  })
  .catch((err: Error | AxiosError) =&gt; {
    if (axios.isAxiosError(err)) {
      return err.response
    } else {
      return err;
    }
  })
});

export const reminderSlice = createSlice({
  name: &quot;reminder&quot;,
  initialState,
  reducers: {
    setReminders: (state, action: PayloadAction&lt;Reminder[]&gt;) =&gt; {
      state.reminders = action.payload;
    },
    deleteReminder: (state, action: PayloadAction&lt;string&gt;) =&gt; {
      state.reminders = state.reminders.filter(
        (reminder) =&gt; reminder.id === action.payload
      );
    },
  },
  extraReducers: (builder) =&gt; {
    builder
    .addCase(fetchReminders.fulfilled, (state, action) =&gt; {
      state.reminders = action.payload;
    })
  }
});

export const { setReminders, deleteReminder } = reminderSlice.actions;

export default reminderSlice.reducer;

答案1

得分: 1

你需要从createAsyncThunkpayloadCreator中返回值。

payloadCreator是:

一个回调函数,应返回一个包含一些异步逻辑结果的 promise。它也可以同步返回一个值。

没有返回值的函数签名是 type fn = () => void;这就是你得到错误的原因。

export const fetchReminders = createAsyncThunk("getReminders", async () => {
  // await axios.get("http://localhost:3000/api/reminders")
  
  return axios.get("http://localhost:3000/api/reminders")
    .then((response) => {
      return response.data.reminders;
    })
    .catch((err: Error | AxiosError) => {
      if (axios.isAxiosError(err)) {
        return err.response
      } else {
        return err;
      }
    })
});
英文:

You need to return the value from payloadCreator of createAsyncThunk.

payloadCreator is:

> A callback function that should return a promise containing the result of some asynchronous logic. It may also return a value synchronously.

The function signature without return value is type fn = () =&gt; void; That's why you got the error.

export const fetchReminders = createAsyncThunk(&quot;getReminders&quot;, async () =&gt; {
  // await axios.get(&quot;http://localhost:3000/api/reminders&quot;)
  
  return axios.get(&quot;http://localhost:3000/api/reminders&quot;)
    .then((response) =&gt; {
      return response.data.reminders;
    })
    .catch((err: Error | AxiosError) =&gt; {
      if (axios.isAxiosError(err)) {
        return err.response
      } else {
        return err;
      }
    })
});

huangapple
  • 本文由 发表于 2023年6月9日 03:42:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定