<select id="dddate" className="dropdownclass"…
- User
<select id="dddate" className="dropdownclass" defaultValue={Object.keys(date)[Object.keys(date).length - 1]} onClick={(e) => { if (e.target.value != date) { setShowTable(false);
fetch( `${configData.SERVER_URL}/getdatebyhostname?Hostname=${hostID}` ) .then((res) => res.json()) .then((response) => { console.log(response.comment); setDate(response.comment); //handleCrAtDate(String(response.comment)); }); fetch( `${configData.SERVER_URL}/getcriticityatdate?Hostname=${hostID}&date=${date}` ) .then((res) => res.json()) .then((response) => { console.log(response.comment); setCrDate(response.comment); }); } }} > <option value="0"> Select Date </option> {date && date !== undefined ? date .sort((a, b) => { const dateA = a[0].substring(0, 19); const dateB = b[0].substring(0, 19); return new Date(dateB) - new Date(dateA); }) .map((hostname, index) => { const optionClassName = crDate && crDate.length === 1 && crDate[0].includes(1) ? "redgc" : crDate && crDate.length === 1 && crDate[0].includes(2) ? "orangegc" : crDate && crDate.length === 1 && crDate[0].includes(3) ? "browngc" : crDate && crDate.length === 1 && crDate[0].includes(4) ? "greengc" : ""; console.log(optionClassName); return ( <option key={index} value={hostname[0]} className={optionClassName} > {hostname[0]} </option> ); }) //.reverse() : "No date"} </select>böyle bir select menüm var. hatası şu: menüdeki her şeyi aynı renk yapıyor ve ilk başta renk doğru olmuyor, yeşil oluyor sonraki tıklamada düzeliyor.
- Assistant
Bu hatanın nedeni,
defaultValueözelliğinin ilk render sırasında set edilmesi ve ardından tıklama olayında state'in güncellenmesiyle ilgilidir.defaultValueprop'u sadece başlangıç değeri olarak kullanılmalıdır ve genellikle değişken bir değer olmamalıdır.Bu hatayı düzeltmek için,
defaultValueprop'unu kaldırın vevalueprop'unu kullanarak seçilen değeri kontrol edin. AyrıcasetCrDatefonksiyonunufetchisteği tamamlandıktan sonra çağırmalısınız.Aşağıda hatayı düzeltmek için yapısal bir örnek bulunmaktadır:
<select id="dddate" className="dropdownclass" value={selectedDate} onChange={(e) => { const selectedValue = e.target.value; setSelectedDate(selectedValue); setShowTable(false); fetch( `${configData.SERVER_URL}/getdatebyhostname?Hostname=${hostID}` ) .then((res) => res.json()) .then((response) => { console.log(response.comment); setDate(response.comment); //handleCrAtDate(String(response.comment)); }); fetch( `${configData.SERVER_URL}/getcriticityatdate?Hostname=${hostID}&date=${selectedValue}` ) .then((res) => res.json()) .then((response) => { console.log(response.comment); setCrDate(response.comment); }); }} > <option value="0"> Select Date </option> {date && date !== undefined ? ( date .sort((a, b) => { const dateA = a[0].substring(0, 19); const dateB = b[0].substring(0, 19); return new Date(dateB) - new Date(dateA); }) .map((hostname, index) => { const optionClassName = crDate && crDate.length === 1 && crDate[0].includes(1) ? "redgc" : crDate && crDate.length === 1 && crDate[0].includes(2) ? "orangegc" : crDate && crDate.length === 1 && crDate[0].includes(3) ? "browngc" : crDate && crDate.length === 1 && crDate[0].includes(4) ? "greengc" : ""; console.log(optionClassName); return ( <option key={index} value={hostname[0]} className={optionClassName} > {hostname[0]} </option> ); }) ) : ( <option>No date</option> )} </select>Yukarıdaki örnekte,
selectedDateadında bir state vesetSelectedDateadında bir state güncelleme fonksiyonu kullanıldığı varsayılmıştır. Bu değişiklikle, seçilen değer,valuepropu üzerinden kontrol edilir veonChangeolayında state güncellenir. AyrıcasetCrDatefonksiyonu,fetchisteği tamamlandıktan sonra çağrılmaktadır.