【问题标题】:React native axios反应原生 axios
【发布时间】:2018-06-15 18:52:48
【问题描述】:

我正在通过 axios 获取数据以进行本机反应,但一直显示这样的错误消息。我该如何解决?提前致谢!

import axios from 'axios';

export default class App extends Component {

  state = {
    persons: []
  }

  async componentDidMount() {
    this.fetchData();
  }

  fetchData = async() =>{
    axios.get(`https://jsonplaceholder.typicode.com/users`)
      .then(res => {
        const persons = res.data;
        this.setState({ persons });
      })
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
        { this.state.persons.map(person => {person.name})}
        </Text>
      </View>
    );
  }
}

我在安装axios时出现错误信息:

npm ERR! code ENOSELF
npm ERR! Refusing to install package with name "axios" under a package
npm ERR! also called "axios". Did you name your project the same
npm ERR! as the dependency you're installing?
npm ERR!
npm ERR! For more information, see:
npm ERR!     <https://docs.npmjs.com/cli/install#limitations-of-npms-install-algorithm>

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/user/.npm/_logs/2018-06-15T19_16_16_279Z-debug.log

【问题讨论】:

  • 如何导入axios?
  • 请编辑您的问题并在此处添加相关信息。
  • 完成。提前致谢。我完全糊涂了。
  • 没问题。你安装了 axios 吗?
  • 显示错误信息

标签: react-native axios


【解决方案1】:

替换

fetchData = async() =>{

与

fetchData = () =>{

您已经在处理 axios Promise,因此您的 async 函数甚至不会返回 Promise,但由于您已将其声明为 async,因此期望返回。你的函数不能是异步的,因为它不做异步的东西。

【讨论】:

    猜你喜欢
    • 2020-11-16
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    相关资源
    最近更新 更多