javascript - 从 axios 请求返回字符串数组填充 react 下拉列表

标签 javascript reactjs axios

我有一个返回字符串数组的 axios get 请求。

class App extends Component {
  //  default state object
  constructor() {
    super();

    this.state = {
      data: []
    };
  }

  componentDidMount() {
    axios
      .get("/dataschema/list")
      .then(response => {
        console.log(response);
        this.setState({ data: response });
      })
      .catch(error => console.log(error.response));
  }
}

当我发出此请求时,Web 控制台显示响应的 data 部分

Response:
{
    "data": [
        "Phone Record"
    ],
}

我的问题是如何获取此字符串并用它填充下拉列表?

对于上下文,整个响应看起来像这样:

{data: Array(1), status: 200, statusText: "OK", headers: {…}, config: {…}, …}
data:Array(1)
   0: "Phone Record"
   length:1
   __proto__: Array(0)

在我的 UserForm.js 类中,我将模式作为 prop 传递

render() {
  const { schemas } = this.props; //schemas references the list received from response and passed as a prop

  return (
    <div>
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick the dataschema to describe your data file:
          <select schemas={this.schemas} onChange={this.handleChange}>
            {schemas &&
              schemas.length > 0 &&
              schemas.map(schema => {
                return <option value="${schema.schemaName}"> </option>;
              })}
          </select>
        </label>{" "}
        <br />
      </form>
    </div>
  );
}

我如何操作响应数据以使用返回的字符串填充下拉列表?

编辑:新的数据形态

Response:
{
    "data": [
        {
            "name": "Phone Record"
        }
    ],

最佳答案

应用程序.js 由于响应是一个巨大的对象,而您只关心数据(数组),因此仅将数据存储为状态的一部分是有意义的。

  componentDidMount() {
    axios
      .get("/dataschema/list")
      .then(response => {
        console.log(response);
        this.setState({ data: response.data });
      })
      .catch(error => console.log(error.response));
  }

用户窗体.js 元素“选项”允许一个子项作为其值属性的掩码,因此设置值属性不会设置选项子项的值(在您的代码中,您只设置值而不是选项的子项)

render() {
  const { schemas } = this.props; //schemas references the list received from response and passed as a prop

  return (
    <div>
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick the dataschema to describe your data file:
          <select onChange={this.handleChange}>
            {schemas &&
              schemas.length > 0 &&
              schemas.map(schema => {
                return <option key={schema} value={schema}>{schema}</option>;
              })}
          </select>
        </label>{" "}
        <br />
      </form>
    </div>
  );
 }

关于javascript - 从 axios 请求返回字符串数组填充 react 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51250618/

相关文章:

javascript - XMLHttpRequest 无法加载 http ://abc. mydomain.org/data/todo.js

javascript - RequireJS 定义回调返回错误的模块依赖项

javascript - Visual Studio Code 根据项目更改语言模式

javascript - 有关如何以及何时在 React 中使用焦点的示例

javascript - 使用通用操作和 reducer 简化 redux

javascript - 使用 javascript 打开下载的 zip 文件时出现问题

javascript - 抛出对象而不是返回字符串

javascript - 如何以不区分大小写的方式查找和替换字符串的一部分?

javascript - 通过 JavaScript 检测应用程序是否在 phonegap 中运行的最有效和可靠的方法

基于javascript的进度条