我有一个输出,我想在数组中显示它最多五个值
我在setSearchHishory中有输出(搜索); 我正在历史上显示它:{search history} 目前,我能够记录当前价值,并在更换PUT的更新值时正在显示。 我希望以前的输出和新输出像一个数组一样显示,最多可容纳五个位置。
const [city, setCity] = useState(null);
const [search, setSearch] = useState("Dehradun");
const [searchHistory, setSearchHistory] = useState([]);
useEffect ( () => {
const fetchApi = async () => {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${search}&units=metric&appid=7938d9005e68d8b258a109c716436c91`
const response = await fetch(url);
const resJson = await response.json();
setCity(resJson.main);
};
fetchApi();
},[search] )
const handleClick = event => {
event.preventDefault();
setSearchHistory(search);
};
return(
<>
<div className="box">
<div className="inputData">
<input
id="btnc"
type="search"
value={search}
className="inputFeild"
onChange={ (event) => { setSearch(event.target.value) }}
/>
<button onClick={handleClick}>Click</button>
</div>
<input className="is"></input>
<h3 className="tempmin_max">
History: {searchHistory}
</h3>
{!city ? (
<p className="errorMsg">Enter City Name</p>
) : (
<div>
<div className="info">
<h2 className="location">
<i className="fa-solid fa-street-view"> </i>{search}
</h2>
<h1 className="temp">
{city.temp}
</h1>
<h3 className="tempmin_max">
Min : {city.temp_min} | Max : {city.temp_max}
</h3>
</div>
<div className="wave -one"></div>
<div className="wave -two"></div>
<div className="wave -three"></div>
</div>
) }
</div>
</>
)
}
export default Tempapp;```
I have output in setSearchHistory(search);
and i am displaying it in History: {searchHistory}
Currently I am able to log current value and on changing out put updated value is getting displayed.
I want the previous output and new output to be displayed like an array and this goes up to five places.
const [city, setCity] = useState(null);
const [search, setSearch] = useState("Dehradun");
const [searchHistory, setSearchHistory] = useState([]);
useEffect ( () => {
const fetchApi = async () => {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${search}&units=metric&appid=7938d9005e68d8b258a109c716436c91`
const response = await fetch(url);
const resJson = await response.json();
setCity(resJson.main);
};
fetchApi();
},[search] )
const handleClick = event => {
event.preventDefault();
setSearchHistory(search);
};
return(
<>
<div className="box">
<div className="inputData">
<input
id="btnc"
type="search"
value={search}
className="inputFeild"
onChange={ (event) => { setSearch(event.target.value) }}
/>
<button onClick={handleClick}>Click</button>
</div>
<input className="is"></input>
<h3 className="tempmin_max">
History: {searchHistory}
</h3>
{!city ? (
<p className="errorMsg">Enter City Name</p>
) : (
<div>
<div className="info">
<h2 className="location">
<i className="fa-solid fa-street-view"> </i>{search}
</h2>
<h1 className="temp">
{city.temp}
</h1>
<h3 className="tempmin_max">
Min : {city.temp_min} | Max : {city.temp_max}
</h3>
</div>
<div className="wave -one"></div>
<div className="wave -two"></div>
<div className="wave -three"></div>
</div>
) }
</div>
</>
)
}
export default Tempapp;```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样做:
现在
search History
中有一个数组。您需要使用MAP
函数显示搜索历史记录。you can do like this:
Now there is a array in
searchHistory
. You need to use themap
function to display the search history.