使动态反应选择组件,没有得到想要的结果,

发布于 2025-01-17 08:36:20 字数 1486 浏览 0 评论 0原文

我想渲染一个动态选择/下拉菜单,我将从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各自安好 2025-01-24 08:36:20

您有多个调查问题,需要有2个组件。 SurveyDropdown用于一个问题。以及包含多个下拉 s的外部组件。

const {useState} = React;
const { FloatingLabel, Form } = ReactBootstrap;

const SurveyDropdown = (props) => {
 //default value
  const [selectedOption, setSelectedOption] = useState();
  function handleSelectChange(event) {
    setSelectedOption(event.target.value);
  }

  return (
    <React.Fragment>
      <FloatingLabel
        controlId="floatingSelect"
        label={props.surveyQuestion}
      >
        <Form.Select onChange={handleSelectChange}>
          {props.option.map(o =>
            <option key={o} value={o} selected={o==selectedOption}>{o}</option>
          )}
        </Form.Select>
      </FloatingLabel>
    </React.Fragment>
  );
};

const Survey = ({data}) => {
  return (
      <React.Fragment>
        {data.map(question =>
          <SurveyDropdown key={question.id} {...question} />
        )}
      </React.Fragment>
    );
}

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"],
    },
];

// Render it
ReactDOM.render(
  <Survey data={surveyData} />,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-bootstrap/2.2.2/react-bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<div id="react"></div>

You have multiple survey questions, you need to have 2 components. SurveyDropdown for a single question. And an outer component containing multiple dropdowns.

const {useState} = React;
const { FloatingLabel, Form } = ReactBootstrap;

const SurveyDropdown = (props) => {
 //default value
  const [selectedOption, setSelectedOption] = useState();
  function handleSelectChange(event) {
    setSelectedOption(event.target.value);
  }

  return (
    <React.Fragment>
      <FloatingLabel
        controlId="floatingSelect"
        label={props.surveyQuestion}
      >
        <Form.Select onChange={handleSelectChange}>
          {props.option.map(o =>
            <option key={o} value={o} selected={o==selectedOption}>{o}</option>
          )}
        </Form.Select>
      </FloatingLabel>
    </React.Fragment>
  );
};

const Survey = ({data}) => {
  return (
      <React.Fragment>
        {data.map(question =>
          <SurveyDropdown key={question.id} {...question} />
        )}
      </React.Fragment>
    );
}

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"],
    },
];

// Render it
ReactDOM.render(
  <Survey data={surveyData} />,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-bootstrap/2.2.2/react-bootstrap.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<div id="react"></div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文