I have a method that uses Axios to connect to an API.
I need to use it twice in my app, but the only way I can figure out how to get it to work is by duplicating a bunch of code as you see below.
I tried calling updateHealthData
in my if
statement, but it says it's not defined.
So the only way I figured out to do it was to call the axios
statement twice.
But this seems like it's duplicating the code too much.
Is there anyway to define it so that I don't have to do it like this?
Thanks!
const App = ({ healthId, healthTerms }) => {
var savedTerms = localStorage.getItem(healthId);
if (healthTerms !== savedTerms) {
const request = axios.put(`/api/healthPortal/${healthId}/healthTerms`, JSON.stringify(savedTerms), {
headers: {
'Content-Type': 'application/json'
}
});
}
const updateHealthData = (i) => {
localStorage.setItem(healthId, i);
const request = axios.put(`/api/healthPortal/${healthId}/healthTerms`, JSON.stringify(i), {
headers: {
'Content-Type': 'application/json'
}
});
}
return ( <TermManager onChange={updateHealthData} /> )
}