简体   繁体   English

useGetOne 响应不会重新渲染组件

[英]useGetOne response doesn't rerender component

I am feeling quite frustrated at this simple example because either I am not getting something or it is not working the way I expect it and the way it says it should in the documentation.我对这个简单的例子感到非常沮丧,因为要么我没有得到一些东西,要么它没有按照我期望的方式工作,也没有按照文档中所说的方式工作。

So I am trying to extend the CloneButton component to actually retrieve the record on each row od the Datagrid and pass it to the create 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} />
};

What I get on the console is just outside null Also when the button is pressed it redirects to the create page with empty record property.我在控制台上得到的只是outside null另外当按下按钮时,它会重定向到具有空record属性的创建页面。

I can see the network request and they all return valid jsons.我可以看到网络请求,它们都返回有效的 jsons。 I don't understand why the component obviusly doesn't rerender when the response is recieved.我不明白为什么组件在收到响应时显然没有重新渲染。 If someone can see what am I missing, I will be extremely grateful!如果有人能看到我错过了什么,我将不胜感激!

The console succeeds, this means that there is neither an error nor a loading .控制台成功,这意味着既没有error也没有loading

After checking the implementation of useGetOne , if it doesn't basically find a resource in your admin state with the given name, it will return null.检查useGetOne实现后,如果基本上没有找到具有给定名称的admin state的资源,它将返回 null。

So I presume that you don't have the resource being declared in the admin's state (aka <Resource name="your name" /> in an <Admin> tree.所以我假设你没有在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
    );

I faced the same issue.我遇到了同样的问题。 Here's what I found.这是我发现的。

When an action has been dispatched, Redux checks if the state has changed.当一个动作被分派时,Redux 检查状态是否已经改变。 If the state has not changed then it doesn't have to update the components.如果状态没有改变,那么它不必更新组件。 When Redux cannot see that the state has changed, it will not update the React components with new data from the state.当 Redux 无法看到状态已更改时,它不会使用来自状态的新数据更新 React 组件。 Reference参考

The returning data is the same JSON which is why the state is not updating.返回的数据是相同的 JSON,这就是状态没有更新的原因。

Two ways to solve this.有两种方法可以解决这个问题。

  1. If you have access to your API, then create a new resource with a different basePath that returns the same result.如果您有权访问您的 API,则创建一个具有不同basePath的新资源,该资源返回相同的结果。 Temporary fix.临时修复。 Not the best solution.不是最好的解决方案。 Ex: <Resource name="cart" /> , <Resource name="cartDetails" />例如: <Resource name="cart" /> , <Resource name="cartDetails" />
  2. Clear State in React - You can follow this article for clearing state in hooks在 React 中清除状态 - 您可以按照这篇文章清除钩子中的状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM