如何从console.log访问此响应?

时间:2018-06-18 18:13:15

标签: reactjs react-native console.log

通常,我使用fetch更容易实现此功能,但此程序包不使用fetch。 我读了类似于我的其他问题,这是我尝试这样做的尝试:

SelectPhoto = () =>{
ImagePicker.openPicker({
}).then((imgResponse) => {
  console.log(imgResponse.path); <-- Trying to access the path
});
}

我一直收到undefined,这是我的控制台日志结果:

0:{size: 745660, mime: "image/jpeg", height: 2960, width: 1440, path: "file:///storage/emulated/0/DCIM/Screenshots/Screenshot_20180617-173313.jpg"} length:1

任何人都可以帮我解决这个问题吗?非常感谢你!

1 个答案:

答案 0 :(得分:1)

看起来你正在使用react-native-customized-image-picker

根据他们的docs和您的console.log输出,您可以获取路径并将其设置为以下状态:

import React from 'react'
import ImagePicker from 'react-native-customized-image-picker'

class App extends React.Component {
  constructor (props) {
    super(props)

    this.state = { path: null }
    this.handleClick = this.handleClick.bind(this)
  }

  handleClick () {
    ImagePicker.openPicker({}).then(image => {
      const path = image[0].path

      this.setState({ path })
    })
  }

  render () {
    return <div onClick={this.handleClick}>Select image</div>
  }
}
相关问题