反应本机axios

时间:2018-06-15 18:52:48

标签: react-native axios

我正在通过axios获取数据用于本机反应但是仍然显示这样的错误消息。我该如何解决?提前谢谢!

enter image description here

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

1 个答案:

答案 0 :(得分:1)

替换

fetchData = async() =>{

使用

fetchData = () =>{

您已经在处理axios Promise,因此您的async函数甚至都不会返回Promise,而是希望这样做,因为您已将其声明为async。您的功能一定不能是异步的,因为它不会做异步的事情。