我正在将分页和查询搜索添加到我的机票列表中

这是票证列表页面代码

  const [query, setQuery] = useState("");

  function handleSearch(e) {
    console.log(e.target.value);
    setQuery(e.target.value);
  }
  useEffect(() => {
    dispatch(getAllTicketsAsync(page, query));
  }, [page, query]);

这是Ticket Slice,我正在使用Redux工具包

export const getAllTicketsAsync = createAsyncThunk(
  "tickets/getAllTickets",
  async (page, query) => {
    const response = await getTickets(page, query);
    // The value we return becomes the `fulfilled` action payload
    return response;
  }

最后,这是API调用

export async function getTickets(page, query) {
  const response = await fetch(
    `http://localhost:8080/tickets?_page=${page}&_limit=4&q=${query}`
  );
  const data = await response.json();
  const totalItems = await response.headers.get("X-Total-Count");
  console.log(data);

  return {
    data,
    totalItems: +totalItems,
  };
}

);

我看不出我在这里做错了什么?查询应该是空字符串如果没有输入任何内容,那么为什么这是请求的url-"http://localhost:8080/tickets?_page=1&_limit=4&q=[object%20Object]"

推荐答案

CreateAsyncThunk接受三个参数:字符串操作类型值、payloadCreator回调和Options对象(可选). 在您的实现中,第二个参数payloadCreator是错误的.

将使用两个参数调用payloadCreator函数:

  • Arg:单一值
  • HunkAPI

要传递2个查询参数,应将它们作为一个对象传递.

export const getAllTicketsAsync = createAsyncThunk(
  "tickets/getAllTickets",
  async ({ page, query }, HunkAPI) => {
    const response = await getTickets(page, query);
    // The value we return becomes the `fulfilled` action payload
    return response;
  }
)

useEffect(() => {
    dispatch(getAllTicketsAsync({ page, query }));
}, [page, query]);

请查看文档here.

Javascript相关问答推荐

使用useup时,React-Redux无法找到Redux上下文值

Chrome是否忽略WebAuthentication userVerification设置?""

WebRTC关闭navigator. getUserMedia正确

函数返回与输入对象具有相同键的对象

未捕获错误:[]:getActivePinia()被调用,但没有活动Pinia.🍍""在调用app.use(pinia)之前,您是否try 使用store ?""

在我的html表单中的用户输入没有被传送到我的google表单中

在open shadow—root中匹配时,使用jQuery删除一个封闭的div类

如何在Node.js中排除导出的JS文件

在css中放置所需 colored颜色 以填充图像的透明区域

搜索功能不是在分页的每一页上进行搜索

一个实体一刀VS每个实体多刀S

如何在Java脚本中对列表中的特定元素进行排序?

Django导入问题,无法导入我的应用程序,但我已在设置中安装了它

Pevent触发material 用户界面数据网格中的自动保存

P5play SecurityError:无法从';窗口';读取命名属性';Add';:阻止具有源的帧访问跨源帧

如何在移动设备中使用JAVASSCRIPT移除点击时的焦点/悬停状态

在Puppeteer中使用promise进行日志(log)记录时出现TargetCloseError

更新文本区域行号

Refine.dev从不同的表取多条记录

使用onClick单击子元素时,使用交点观察器的关键帧动画意外运行