Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing data on another page by clicking the row: It shows that data is null

I am using MUI-Datatable. And it can already go to the next page, however, no data is showing in History page. How can I fix this?

Passing data to another page:

Here, I can see the data in the console

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  const handleRowClick = (rowData, rowMeta) => {
    navigate("/history", rowData[0]);
    console.log(rowData[0], "products");
  };

  const options = {
    filter: true,
    selectableRows: "none",
    responsive: "simple",
    onRowClick: handleRowClick,
  };

History page:
Nothing shows in the state

const History = (props) => {
  const { state } = useLocation(); //document ID here
  console.log(state, "state");


  return (
    <div>
      
    </div>
  );
};

export default History;

App.js

   <Route
            path="/history"
            element={
              <PrivateRoute>
                <Layout>
                  <History />
                </Layout>
              </PrivateRoute>
            }
          />

I do not know what is the problem here since I can just pass another data to my edit-page whenever I’ll click the button edit which is present in each row.

>Solution :

the navigate function takes up to two arguments, the "to" target and an "options" object with state and replace keys.

useNavigate

declare function useNavigate(): NavigateFunction;

interface NavigateFunction {
  (
    to: To,
    options?: { replace?: boolean; state?: any } // <-- options object, state
  ): void;
  (delta: number): void;
}

Move the rowData array element into the options object under the state key.

Example:

navigate("/history", { state: { rowData: rowData[0] } });

Use the useLocation hook on the receiving component to access the route state.

const { state } = useLocation();
const { rowData } = state || {};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading