将API派生的值映射到React.js状态时出现问题

哈克布里奇

我是使用React / Axios的新手。

我正在尝试通过Axios将城市词典API中的数据存储到状态中的空数组中。

然后,我试图通过render函数将每个定义的名称呈现给浏览器。


  constructor(props) {
    super(props);
    this.state = {
      definitions: [],
      errors: null
    };
  }

  componentDidMount() {
    this.getUrbDef();
  }

  getUrbDef() {
    axios({
      method: 'get',
      url: 'https://mashape-community-urban-dictionary.p.rapidapi.com/define',
      headers: {'host': 'mashape-community-urban-dictionary.p.rapidapi.com', 'X-RapidAPI-Key': 'keydata'},
      params: {'term': 'world'}
    })
    .then(function (response) {
      console.log(response);
    })
    .then(response =>
      response.data.list.map(definition => ({
        name: `${definition.definition}`,
        id: `${definition.defid}`,
      }))
    )
    .then(definitions => {
      console.log(definitions);
      this.setState({
        definitions,
        isLoading: false
      });
      })
    .catch(error => this.setState({ error, isLoading: false }));
  }

  // Renders to the browser
  render() {
    // Grabbing objects to use from state
    let definitions = this.state.definitions;

如前所述,这是我第一次以这种方式使用React.js,因此我有些困惑-任何帮助将不胜感激!

演示:https : //codesandbox.io/s/stoic-neumann-28rhe

El Aoutar Hamza

问题在于then处理程序未返回任何内容:

.then(function (response) {
  console.log(response);
})

说明

调用then返回一个Promise,该Promise将根据处理程序函数内部发生的事情而得到解决/拒绝。

在您的情况下,处理程序未返回任何内容(console.log(response)),因此,由返回的promisethen将使用未定义的值进行解析,结果是:

.then(response =>
  response.data.list.map(definition => ({
    name: `${definition.definition}`,
    id: `${definition.defid}`,
  }))
)

接收未定义。

您可以采取以下措施来解决此问题:

1)返回response内部then处理程序:

.then(function (response) {
  console.log(response);
  return response;
})

2)then从链条中删除第一个

axios({
  method: 'get',
  url: 'https://mashape-community-urban-dictionary.p.rapidapi.com/define',
  headers: {'host': 'mashape-community-urban-dictionary.p.rapidapi.com', 'X-RapidAPI-Key': 'keydata'},
  params: {'term': 'world'}
})
.then(response =>
  response.data.list.map(definition => ({
    name: `${definition.definition}`,
    id: `${definition.defid}`,
  }))
)

然后

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将项目添加到钩子状态反应js时出现问题

遍历数组时从状态访问值时出现问题

在React中从子组件更新父状态时出现问题

将useState的状态分配给文字类型(HTMLSelectElement)时出现问题

尝试将接口映射到Unity中的类类型时出现问题

将新表数据映射到现有表时出现问题

将值从链接传递到JS文件时出现问题

React.js:更新多个组件共享的上下文API时出现问题

使用React.JS提取API数据时出现问题

在 App.js React 开发中导入外部 Js 时出现问题

尝试使用React.js中的map访问特定JSON数据时出现问题

使用React.js渲染MDL文本字段时出现问题

React.js CKEditor5组件-创建组件时出现问题

尝试将HTML文件链接到下拉链接时出现问题(无JS)

尝试将翻译键值的json从laravel刀片传递到vue.js时出现问题

尝试将 css 文件导入 vue.js 时出现问题

使用AJAX用PHP将JS变量插入MySQL表时出现问题

将.split()结果分配给名为“ name”的变量时,JS出现问题

将脚本重写为原始js(不是jQuery)时出现问题

node.js:使用系统调用将文件写入/ tmp目录时出现问题

将值与PDO绑定,插入IS NULL时出现问题

将文件发送到API时出现问题

在JPA /休眠模式下映射UUID时出现问题

React.js:将<input type =“ file”>转换为自定义按钮图标时出现问题

计算值时出现问题,不返回任何内容

打印指针的取消刷新值时出现问题

在arraylists java中组合值时出现问题

在javascript中查找重复值时出现问题

在函数中返回几个值时出现问题?