小编Ded*_*pul的帖子

如何在渲染函数中调用函数?

如果我尝试调用该fetchToken()函数,它只是说它不是一个函数.如果我把它放在渲染功能之外this.propsundefined,我无法调用它.

class LoginPage extends Component {


    componentDidMount() {
        Linking.addEventListener('url', this.handleOpenURL);
    }
    componentWillUnmount() {
        Linking.removeEventListener('url', this.handleOpenURL);
    }
    handleOpenURL(event) {
        let code = event.slice(22,86);
        console.log(code);
        this.fetchToken(code)
    }
  
  render() {

        function fetchToken(code) {
            this.props.actions.fetchToken(code)
        }
        
        return (
            <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                <TouchableHighlight style={{backgroundColor: '#9b59b6', height: 70, padding: 20}} onPress={this.openAuth.bind(this)}>
                    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                        <Text style={{color: 'white', fontSize: 16}}>Authenticate with Dribbble</Text>
                    </View>
                </TouchableHighlight>
            </View>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

react-native

10
推荐指数
2
解决办法
9372
查看次数

没有带fetch()的JSON对象

我设置了oauth。但是,当我想使用fetch()函数获取访问令牌时,它只会返回一个带有_bodyInit,_bodyBlob和标头之类的对象。因此,我只是无法获取JSON对象。无论如何,我都在使用Android。

码:

componentDidMount() {
Linking.getInitialURL().then(url => {
      if(url) {
        console.log(url);
        const queries = url.substring(16)
        const dataurl = qs.parse(queries);
        if(dataurl.state === 'ungessable15156145640!') {
          console.log(dataurl.code);
          console.log(dataurl.state);
          return code = dataurl.code;
        }
      }
    }).then((code) => {
      fetch(`https://dribbble.com/oauth/token`, {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          'client_id': 'MY_ID',
          'client_secret': 'MY_SECRET',
          'code': code
        })
      })
      .then((res) => {
        var access_token = res;
        console.log(access_token);
      });
    });
  }
Run Code Online (Sandbox Code Playgroud)

javascript react-native fetch-api

3
推荐指数
1
解决办法
2208
查看次数

标签 统计

react-native ×2

fetch-api ×1

javascript ×1