使动态反应选择组件,没有得到想要的结果,
我想渲染一个动态选择/下拉菜单,我将从 api 获取它作为模板,我试图传递动态数据来反应选择语句,但仍然没有得到准确的响应。 我想显示 n 个从 api 接收的下拉列表。下拉列表的标签将作为问题,选项将作为答案。 有人可以帮我弄清楚什么是正确的方法。 下面是我的功能组件。 谢谢!
export const SurveyDropdown = () => {
const [selectedSurvey, setselectedSurvey] = useState(); //default value
function handleSelectChange(event) {
setselectedSurvey(event.target.value);
}
console.log(selectedSurvey)
const surveyData = [
{
id: 1,
surveyQuestion: "Question One",
type: "dropdown",
option: ["optionOne", "optionTwo", "optionThree"],
},
{
id: 2,
surveyQuestion: "Question Two",
type: "dropdown",
option: ["optionOne", "optionTwo", "optionThree"],
},
{
id: 3,
surveyQuestion: "Question Three",
type: "button",
option: ["optionOne", "optionTwo", "optionThree"],
},
];
let optionTemplate = surveyData.map(surveyData => (
<option value={surveyData.option} key={surveyData.surveyQuestion}>{surveyData.option.values}</option>
));
console.log(optionTemplate);
return (
<>
<FloatingLabel
controlId="floatingSelect"
label="This is question"
value={selectedSurvey}
onChange={handleSelectChange}
>
<Form.Select aria-label="Floating label select example">
<option>{optionTemplate}</option>
</Form.Select>
</FloatingLabel>
</>
);
};
I want to render a dynamic select/dropdown which I will fetch from an api as a template, I'm trying to pass on dynamic data to react select statement, still not getting exact response.
I want to display n number of dropdown, which be received from an api. Label of the dropdown will server as a question and options will be the answers.
Can someone help me figure out what shall be correct approach.
Below is my functional component for the same.
Thanks!
export const SurveyDropdown = () => {
const [selectedSurvey, setselectedSurvey] = useState(); //default value
function handleSelectChange(event) {
setselectedSurvey(event.target.value);
}
console.log(selectedSurvey)
const surveyData = [
{
id: 1,
surveyQuestion: "Question One",
type: "dropdown",
option: ["optionOne", "optionTwo", "optionThree"],
},
{
id: 2,
surveyQuestion: "Question Two",
type: "dropdown",
option: ["optionOne", "optionTwo", "optionThree"],
},
{
id: 3,
surveyQuestion: "Question Three",
type: "button",
option: ["optionOne", "optionTwo", "optionThree"],
},
];
let optionTemplate = surveyData.map(surveyData => (
<option value={surveyData.option} key={surveyData.surveyQuestion}>{surveyData.option.values}</option>
));
console.log(optionTemplate);
return (
<>
<FloatingLabel
controlId="floatingSelect"
label="This is question"
value={selectedSurvey}
onChange={handleSelectChange}
>
<Form.Select aria-label="Floating label select example">
<option>{optionTemplate}</option>
</Form.Select>
</FloatingLabel>
</>
);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有多个调查问题,需要有2个组件。
SurveyDropdown
用于一个问题。以及包含多个下拉
s的外部组件。You have multiple survey questions, you need to have 2 components.
SurveyDropdown
for a single question. And an outer component containing multipledropdown
s.