How to fix my React Context to render useEffect and reload table data

woof

I have an input form that selects data to be rendered inside my tables. The data only appears after a forced page reload. I would like for the data to re-render with the expected data entered by the user after each submit in my input.

To do so I have:

  • an input function called SymbolInput() that sends user requested data to my database.
  • a table function, PriceUpdate() that loads data from my database via useEffect.
  • createContext as the transmitter to reload the table via useEffect dependency array if data has been sent to my database and cause a re-render based on state change.

The problem: I am not getting any errors and I know I'm doing something wrong, I cannot wrap my head around context API for my specific use-case. Hence I would greatly appreciate some clarification for myself to understand useContext and its provider.

Here is my code:

Below I initiate my createContext:

import { createContext } from 'react';

export const TableRefreshContext = createContext();
export default TableRefreshContext;

Then import the context to my file which contains my input form. Here I use state as a trigger/key to re-render my table. If data is sent to my db, then the state key increments by one, to hopefully fire off the useEffect dependency array in my table function.

import TableRefreshContext from '../../../context/TableRefresh';

export default function SymbolInput()
{
  const { control, handleSubmit } = useForm();
  const [refreshKey, SetRefreshKey] = useState([0])

  const onSubmit = (data, e) =>
  {  

    axiosInstance
      .patch(URL, {
        stock_list: data.stock_list.map(list=>list.symbol),
      })
      .then((res) =>
      {
        SetRefreshKey(refreshKey + 1);
      });
  };

  return (
    <TableRefreshContext.Provider value={refreshKey}>
            <form> 
            </form> 
    </TableRefreshContext.Provider>
    );
}

Here is the table function I speak of, where I have a useEffect to pull data from my db, along with it's dependency array.

export function PriceUpdate()
{
    const [data, setData] = useState([]);
    const reloadTable = useContext(TableRefreshContext)
    useEffect(() =>
    {
        const fetchData = async () =>
        {
            const response = await axiosInstance.get(URL)
            const result = response.data;
            setData(result);
        };

        fetchData();
    }, [reloadTable]);

    return (
        <>
            <div>
            <TableRefreshContext.Provider value={reloadTable}>
                <PriceUpdates data={data[0]} />
                <PriceRanges data={data[0]} />
                <ReturnRates data={data[1]} />
                <QuickFacts data={data[2]} />
                <Recommendations data={data[4]} />
                <AdvancedStats data={data[3]} />
            </TableRefreshContext.Provider>

How can I link up my input and table functions together to allow for my page to render new data without having to reload my entire page?

EDIT: Here is how I use PriceUpdate component, it's one of the children of my layout component:


    const reloadTable = useContext(TableRefreshContext)

    return (
            <TableRefreshContext.Provider value={reloadTable}>
                <PriceUpdate />
            </TableRefreshContext.Provider>
    );
}
kunquan

From the current code that you have given, I don't think there is anything wrong with it, except for the refreshKey that should be a number instead of an array. I created a CodeSandbox project that is almost identical to your app and found nothing wrong with it (note that this way of using Context is not recommended as it should be in a higher hierarchy, as mentioned in a previous post).

If your code still not work, then try to create a CodeSandbox project that can reproduce your problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to reload Array Data in React

From Dev

How do I reload data in my TableView

From Dev

How to reload the data of a table with vue and axios?

From Dev

React: how to render data into a component?

From Dev

React data components table does not render HTML

From Dev

Javascript + React :- Unable to put the data in table and then render it

From Java

How to fix missing dependency warning when using useEffect React Hook?

From Dev

How to fix this warning: "React Hook useEffect has a missing dependency: 'history'"?

From Dev

React - how can I get updated data to show in my table?

From Dev

how to render context to my template, what am i missing

From Dev

how to reload the data in data table in webix? refresh() is not reloading the data?

From Dev

How to render programmatically created table in React?

From Java

React Context: TypeError: render is not a function

From Dev

React Context - Context and Child render are not updating

From Dev

How to share data between render and componentDidMount in React?

From Dev

How to fix accidentally duplicated data in a BigQuery table?

From Java

Make React useEffect hook not run on initial render

From Dev

useEffect in React unable to e-render

From Dev

How to fetch data and store in React Context API?

From Dev

How to reload data in table view by tapping the cell of collection view in swift

From Dev

How to reload Table View with different data from the original?

From Dev

Notify table view to reload data

From Dev

Table not reload data until Scroll

From Dev

Notify table view to reload data

From Dev

CCKNavDrawer reload table data issue

From Dev

Jquery data table reload on event

From Dev

Not able to Render Data table

From Dev

Fix-data-table - How to set table fluid width

From Dev

How do I render json in my react component?

Related Related

  1. 1

    How to reload Array Data in React

  2. 2

    How do I reload data in my TableView

  3. 3

    How to reload the data of a table with vue and axios?

  4. 4

    React: how to render data into a component?

  5. 5

    React data components table does not render HTML

  6. 6

    Javascript + React :- Unable to put the data in table and then render it

  7. 7

    How to fix missing dependency warning when using useEffect React Hook?

  8. 8

    How to fix this warning: "React Hook useEffect has a missing dependency: 'history'"?

  9. 9

    React - how can I get updated data to show in my table?

  10. 10

    how to render context to my template, what am i missing

  11. 11

    how to reload the data in data table in webix? refresh() is not reloading the data?

  12. 12

    How to render programmatically created table in React?

  13. 13

    React Context: TypeError: render is not a function

  14. 14

    React Context - Context and Child render are not updating

  15. 15

    How to share data between render and componentDidMount in React?

  16. 16

    How to fix accidentally duplicated data in a BigQuery table?

  17. 17

    Make React useEffect hook not run on initial render

  18. 18

    useEffect in React unable to e-render

  19. 19

    How to fetch data and store in React Context API?

  20. 20

    How to reload data in table view by tapping the cell of collection view in swift

  21. 21

    How to reload Table View with different data from the original?

  22. 22

    Notify table view to reload data

  23. 23

    Table not reload data until Scroll

  24. 24

    Notify table view to reload data

  25. 25

    CCKNavDrawer reload table data issue

  26. 26

    Jquery data table reload on event

  27. 27

    Not able to Render Data table

  28. 28

    Fix-data-table - How to set table fluid width

  29. 29

    How do I render json in my react component?

HotTag

Archive