Here in this React component I am declaring a simple variable called companyData
and assigned default value as "james"
. And then updated the value of companyData
with a new value that I received from axios request.
Though in log, I do see updated value, but within return space (JSX), it gives only old delcared value "james"
on loading component.
How can I make sure only the latest value get displayed?
function Home(props) {
var companyData = "james";
axios
.post(
"https://…..",
{
withCredentials: true,
headers: {
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: 0,
},
},
{
auth: {
username: "",
password: "",
},
data: {
action: "asdf",
},
}
)
.then(function (response) {
companyData = response.data;
console.log(companyData); // it returs updated data received from axios post
})
.catch(function (error) {
console.error(error);
});
return (
<View>
<Text>
{companyData} // here I still received old value james as output
<Text>
<View>
)
}
export default Home;
3
Answers
Replace your variable with a state (useState hook) because when
Home
gets called/recalled, regular variables are reinitilized sinceHome
is a function after all.Now to fetch data when the component mounts we usually use useEffect for that, and with an empty dependency array
[]
, we make the effect runs only once, after the component first render.What will happen is that during the first render "james" is displayed since JSX is returned before
useEffect
runs, but you will not notice it because just after thatuseEffect
will run and updates the state so the component rerenders and now the new value assigned tocompanyData
is displayed during this new render.Good to know:
If you want to use a variable that you don’t want to lose its value when the component rerenders you can make use of useRef
You need to use React states. States should be used when you are rendering something that will change.
How to use
do
do
There are many quirks about states so you should read up on it (or watch a Youtube video), but here is what your modified code should look like:
In react we usually use "useState" to make variables.
useState returns two values, the variable and a function that is used to change the variable. It writes like this :
here the initial variable is "james"
the change it you have to use the function "setCompanyData" like so :
so you final code should look something like this :