I have a form with next fields:
- A drop-down list that allows to choose a branch + A city that is automatically selected according to the branch selected.
- A calendar component that allows to select a date + A details about the date that automatically displays, such as whether it is a work day or a holiday.
- Filleds with the temperature details that automatically displays, depending on the city of the branch and date selected. It updated whenever either of them changes, and both are validted.
The check if the first two fields are validted, getting the temperature from API, and update the temperature fields in temperature (using useState), are done in useEffect. The problem is that every time there is a change in the first two fields, the API call is made two or three times.
I deleted the <React.StrictMode> and it doesn't help.
import Calendar from "../components/Calendar"; import Languages from "../components/Languages/Languages.json"; import React, { useContext, useEffect, useRef, useState } from "react"; import { ContextCities } from "../Contexts/ContextCities"; import { ContextLanguages } from "../Contexts/ContextLanguages"; import { loadBranches } from "../utils/branchesHandelApi"; import { loadWeather } from "../utils/weatherHandelApi"; export default function Transactions() { const { cities } = useContext(ContextCities); const { selectedLanguage } = useContext(ContextLanguages); const [branch, setBranch] = useState([]); const [selectedBranchIndex, setSelectedBranchIndex] = useState(0); const [selectedDate, setSelectedDate] = useState(); const [selectedDateData, setSelectedDateData] = useState(); const [weather, setWeather] = useState(0); const handleSubmitTransaction = async (e) => { e.preventDefault(); // saveTransactions(transaction); }; useEffect(() => { loadBranches(setBranch); }, []); const isInitialMount = useRef(true); useEffect(() => { if (isInitialMount.current) { isInitialMount.current = false; } else { if ( cities !== "undefined" && selectedBranchIndex !== 0 && selectedDate ) { let station_id = cities.cities.find( (itm) => itm.Name === branch[selectedBranchIndex - 1].city ).meteorological_station.id; console.log("station_id", station_id); loadWeather(station_id, selectedDate, setWeather); } else setWeather(0); } }); return ( <> <h1> <span style={{ fontSize: "2rem", fontFamily: "Noto Color Emoji" }}> {"\ud83d\udcb3"} </span>{" "} {Languages.transactions[selectedLanguage]} </h1> <form autoComplete="off" className="App" id="form" onSubmit={(e) => handleSubmitTransaction(e)} > <fieldset> <legend> <h2>{Languages.manual_update[selectedLanguage]}</h2> </legend> <h2>{Languages.branches[selectedLanguage]}</h2> <h3>{Languages.name[selectedLanguage]}:</h3>{" "} <select defaultValue="" name="branch" required onChange={(e) => setSelectedBranchIndex(e.target.selectedIndex)} > <option hidden value=""> {Languages.name[selectedLanguage]} </option> {branch.map((itm, index) => ( <option key={index}>{itm.name}</option> ))} </select>   <h3>{Languages.city[selectedLanguage]}:</h3>{" "} <input disabled="disabled" readOnly value={ selectedBranchIndex === 0 ? "" : branch[selectedBranchIndex - 1].city } /> <br /> <br /> <h2>{Languages.date[selectedLanguage]}</h2> <Calendar setSelectedDate={setSelectedDate} setSelectedDateData={setSelectedDateData} /> <br /> <br /> <h2>{Languages.weather[selectedLanguage]}</h2> <h3>{Languages.meteorological_station_id[selectedLanguage]}:</h3>{" "} <input readOnly disabled="disabled" value={ selectedBranchIndex === 0 || cities === "undefined" ? "" : cities.cities.find( (itm) => itm.Name === branch[selectedBranchIndex - 1].city ).meteorological_station.id } />   <h3> {Languages.meteorological_station_location[selectedLanguage]}: </h3>{" "} <input readOnly disabled="disabled" value={ selectedBranchIndex === 0 || cities === "undefined" ? "" : cities.cities.find( (itm) => itm.Name === branch[selectedBranchIndex - 1].city ).meteorological_station.name } />   <h3>{Languages.temperature[selectedLanguage]}:</h3>{" "} <input readOnly disabled="disabled" value={weather} /> {" \u2103"} </fieldset> </form> </> ); } 
useEffectdependency array that is either changing a lot, or is "complex" in some way (e.g it is a (JSON) Object).useEffectdoesn't have a dependency array