1
I have in the file App.js
And the first time it’s loaded, it calls the const response = await api.get('/searchDate');
, and returns a list of dates
. I want to take the first position on the list, call the file Transaction js. and search on the back, the information according to the date[0]
.
I could do this?
Observing: Every time a new date
the file is selected Transaction js. is called and sends the dateSelected
and searches the information. With the call on html
in the return
<Transaction dateSelected={dateSelected} />
.
However, I wanted to call it the first time it loads the page, with the first date
of the list.
App.js file
function App() {
const [dates, setDates] = useState([]);
const [dateSelected, setDateSelected] = useState();
useEffect(() => {
async function loadDates(){
const response = await api.get('/searchDate');
setDates(response.data.yearMonth);
};
console.log(dates[0]); // aqui ele mostra certinho o date da primeira posição - ele seria o dateSelected
// Como chamar o function Transaction({dateSelected}) {
loadDates();
},[dates[0]]);
const handleChange = (event) => {
setDateSelected(event.target.value);
};
return (
<div id="app">
<main>
<Transaction dateSelected={dateSelected} />
</main>
</div>
);
}
Arquivo Transaction.js
function Transaction({dateSelected}) {
const [transactions, setTransactions] = useState([]);
useEffect(() => {
async function loadTransaction() {
const response = await api.get('/search?yearMonth='+dateSelected);
setTransactions(response.data.transaction);
};
loadTransaction();
}, [dateSelected]);
the component needs a state and that state controls whether it is loaded 1 or more times! only this I think is necessary, now is changing is because you in the same code changes the state or maybe catches a state that is changing and so the file loading.
– novic