簡體   English   中英

useGetOne 響應不會重新渲染組件

[英]useGetOne response doesn't rerender component

我對這個簡單的例子感到非常沮喪,因為要么我沒有得到一些東西,要么它沒有按照我期望的方式工作,也沒有按照文檔中所說的方式工作。

所以我試圖擴展 CloneButton 組件以實際檢索 Datagrid 每一行上的記錄並將其傳遞給 create 組件:

import React from 'react';
import PropTypes from 'prop-types';
import { CloneButton, useGetOne } from 'react-admin';
import CircularProgress from '@material-ui/core/CircularProgress';

const CloneQueryButton = ({
    label = "Duplicate",
    basePath,
    record,
    ...props
}) => {

const { data, loading, error } = useGetOne(basePath, record.id);

if (loading) { return <CircularProgress />; }
if (error) { return <p>ERROR {error.message}</p>; }

if (data) {
    console.log("inside", data);
    data.name = `Copy of ${data.name}`;
}

console.log("outside", data);

return <CloneButton
    basePath={basePath}
    record={data}
    label={label}
    {...props} />
};

我在控制台上得到的只是outside null另外當按下按鈕時,它會重定向到具有空record屬性的創建頁面。

我可以看到網絡請求,它們都返回有效的 jsons。 我不明白為什么組件在收到響應時顯然沒有重新渲染。 如果有人能看到我錯過了什么,我將不勝感激!

控制台成功,這意味着既沒有error也沒有loading

檢查useGetOne實現后,如果基本上沒有找到具有給定名稱的admin state的資源,它將返回 null。

所以我假設你沒有在admin's state聲明的資源(又名<Resource name="your name" /><Admin>樹中。

onst useGetOne = (
    resource: string,
    id: Identifier,
    options?: any
): UseGetOneHookValue =>
    useQueryWithStore(
        { type: 'getOne', resource, payload: { id } },
        options,
        (state: ReduxState) =>
            state.admin.resources[resource]
                ? state.admin.resources[resource].data[id]
                : null
    );

我遇到了同樣的問題。 這是我發現的。

當一個動作被分派時,Redux 檢查狀態是否已經改變。 如果狀態沒有改變,那么它不必更新組件。 當 Redux 無法看到狀態已更改時,它不會使用來自狀態的新數據更新 React 組件。 參考

返回的數據是相同的 JSON,這就是狀態沒有更新的原因。

有兩種方法可以解決這個問題。

  1. 如果您有權訪問您的 API,則創建一個具有不同basePath的新資源,該資源返回相同的結果。 臨時修復。 不是最好的解決方案。 例如: <Resource name="cart" /> , <Resource name="cartDetails" />
  2. 在 React 中清除狀態 - 您可以按照這篇文章清除鈎子中的狀態

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM