Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table is not refreshed with remote data #859

Open
b3ni opened this issue Aug 1, 2024 · 4 comments
Open

Table is not refreshed with remote data #859

b3ni opened this issue Aug 1, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@b3ni
Copy link
Contributor

b3ni commented Aug 1, 2024

Description

When the data attribute is a function and this callback is changed, the data is not refreshed again.

Demo

https://codesandbox.io/s/material-table-starter-template-forked-cxqmwq?file=/src/index.js

Expected behavior

Make a remote call again

Additional context

I think the problem is in the componentDidUpdate method there is no else section

if (!this.isRemoteData()) {
     propsChanged = propsChanged || !deepEql(prevProps.data, this.props.data);
}
@b3ni b3ni added the bug Something isn't working label Aug 1, 2024
@KippWade
Copy link
Contributor

KippWade commented Aug 1, 2024

You can set up a ref for the table, such as tableRef = useRef()

in the table props, you set the ref
tableRef={tableRef}

In other locations in your component, you can trigger a refresh by using the ref

tableRef.current.onQueryChange();

Our remote data is set up like:

data={query =>
new Promise<QueryResult>((resolve, reject) => {
// query holds usefull table info, error, filters, orderByCollection, page, pageSize, search, totalCount

	const fetchList = async () => {
		// perform query
			if (response !== undefined) {
				resolve({
					data: response.results,
					page: response.page,
					totalCount: response.totalCount
				});
			} else {
				reject();
			}
		));
	};
	fetchList();
})

}

@b3ni
Copy link
Contributor Author

b3ni commented Aug 2, 2024

In other parts of the application I use onQueryChange without any problem.

The problem comes when the asynchronous function is changed.

For example, I have also tried:

const { id } = useParams();  // react-router-dom
const url = `/api/v1/endpoint/${id}`;

const fetchRemote = useCallback(async (query) => {
    try {
        const response = await fetch(url);
        const data = await response.json();
        return data; // data, page, totalCount
    } catch (error) {
        ...
    }
}, [id]);

...
data={fetchRemote}

Another test has been using effect. But the problem is that I have 2 calls to the api when the page is rendered for the first time.

const { id } = useParams();
const ref = useRef(null);

useEffect(() => {
    ref.current?.onQueryChange();    
}, [id]);

...
ref={ref}
data={createFetchRemote(`/api/v1/endpoint/${id}`)}

@Domino987
Copy link
Contributor

Yrs i think the callback is only called if something is the query changes, not if the callback changes. This is dine because people to not memorize their functions and this would result in infinity loops.

@KippWade
Copy link
Contributor

KippWade commented Sep 1, 2024

If I remember correctly, from an experiment I did some time ago, In the parens for onQueryChange(here), I THINK you can put in the updated data class.
{
data: updatedArray
page: #,
totalCount: #
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants