从下拉列表中动态创建选项
因此,我正在尝试动态创建选择下拉菜单的选项,我将API与我的国家状态进行获取,但是我不知道如何访问每个对象内的内容
。下面,数据正在从API中提取,也就是说,获取工作,但是我不知道如何创建每个对象中选择的选项。
import { EmailIcon, LocationIcon } from './assets/FormSvgIcons'
import { useEffect, useState } from 'react';
const SettingsForm = () => {
const [stateList, setStateList] = useState([]);
const [userLocation, setUserLocation] = useState('');
const handleLocation = () => {
setUserLocation(e.target.value);
}
useEffect(() => {
let initialStates = [];
fetch('https://servicodados.ibge.gov.br/api/v1/localidades/estados/')
.then(response => {
return response.json();
}).then(data => {
initialStates = data.map((states) => {
return states
});
console.log(initialStates);
setStateList({states: initialStates});
});
}, []);
const createDropdownOptions = () => {
const createOptions = stateList.map((state, i) => {
Object.keys(state).map(singleState => (
<option value={i}>{singleState.sigla}</option>
))
});
return createOptions;
}
return (
<form>
<div className="user-country">
<label className="white-label">
Local
</label>
<div className="input-icon-wrapper">
<div className="icon-input w-embed">
<LocationIcon />
</div>
<select
className="select-field white-select w-select"
id="locationField"
name="locationField"
onChange={handleLocation}
>
{createDropdownOptions()}
</select>
</div>
</div>
</form>
)
我知道错误在createRopDownOptions
函数中,因为它负责创建选项,但是我不知道该怎么做,有光吗?
So, I'm trying to dynamically create the options of a select dropdown, I make the fetch of an api with the states of my country, but I don't know how to access the content inside each object..
As you can see below, the data is being pulled from the API, that is, the fetch worked, but I don't know how to create the options that will be inside the Select with each object..
import { EmailIcon, LocationIcon } from './assets/FormSvgIcons'
import { useEffect, useState } from 'react';
const SettingsForm = () => {
const [stateList, setStateList] = useState([]);
const [userLocation, setUserLocation] = useState('');
const handleLocation = () => {
setUserLocation(e.target.value);
}
useEffect(() => {
let initialStates = [];
fetch('https://servicodados.ibge.gov.br/api/v1/localidades/estados/')
.then(response => {
return response.json();
}).then(data => {
initialStates = data.map((states) => {
return states
});
console.log(initialStates);
setStateList({states: initialStates});
});
}, []);
const createDropdownOptions = () => {
const createOptions = stateList.map((state, i) => {
Object.keys(state).map(singleState => (
<option value={i}>{singleState.sigla}</option>
))
});
return createOptions;
}
return (
<form>
<div className="user-country">
<label className="white-label">
Local
</label>
<div className="input-icon-wrapper">
<div className="icon-input w-embed">
<LocationIcon />
</div>
<select
className="select-field white-select w-select"
id="locationField"
name="locationField"
onChange={handleLocation}
>
{createDropdownOptions()}
</select>
</div>
</div>
</form>
)
I know that the error is in the createDropdownOptions
function because it is responsible for creating the options, but I don't know how to do it, any light?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看到您的问题,您的逻辑是正确的,但是实现不佳,一旦您过滤了数据,它才会呈现一个新组件:
我建议为每个选项使用一个组件,如果您以后需要使用一个组件,那么它将变得更加容易对
I see your problem, your logic is correct, but it is poorly implemented, once you have filtered the data, it is only rendering a new component:
I recommend using a component for each option, this will make it easier if you later need to do some action on the
首先,您可以简化
使用
到下面的代码。当您制作MAP
时,回调返回每个迭代的相同对象,更好地使用data
,因为输出将相同。然后将
CreateRopDownOptions
更改为以下代码。您可以更改value
或显示给nome
的内容:finnaly您需要将事件传递给
handlelelocation
:First you could simplify your
useEffect
to the code below. As you are making amap
where the callback returns the same object for each iteration, better you usedata
as it's, because the output would be the same.Then change
createDropdownOptions
to the code below. You can change thevalue
or what's displayed tonome
:And finnaly you would need to pass the event to
handleLocation
:不要过度思考。提示:
如果您有数组,则可以轻松地将其映射到JSX中并生成您的选项。
您做得很好,非常接近。看看我为使其工作所做的更改:
希望它有帮助!保持良好的工作,并随时伸出手,以防您仍然卡住!
Don't overthink. Tips:
If you have an array, you can easily map it into jsx and generate your options.
You did very well, and got really close. Take a look at the changes I've done to get it working:
Hope it helps! keep up the good work and feel free to reach out in case you're still stuck!