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>
);
}