【问题标题】:React: cant handle async function on event [duplicate]反应:无法处理事件的异步功能[重复]
【发布时间】:2017-10-07 13:50:31
【问题描述】:

当我尝试在h2 中的第二个文件的 onClick 事件上触发异步函数时遇到问题。 我不知道问题出在哪里。我发现了 TypeError: Cannot read property 'props' of undefined. 类型的错误 看看代码就知道了。

import React from 'react'
import {AngularIntroduce} from './../components/AngularIntroduce'
import {connect} from 'react-redux'

export default connect(
state => ({
    angularData: state.angularData,
    angularReposList: state.angularReposList
}),
dispatch =>  ({
    success: data => dispatch({
        type: 'angularData/FETCH__SUCCESS',
        data: data
    })

})

)(class angularRepoList extends React.Component {



componentDidMount() {

    this.getAngularData()


}

async getAngularData() {
    try {
        const response = await             
fetch('https://api.github.com/orgs/angular');
        const got = await response.json();
        await this.props.success(got)
        console.log(this.props.success)
    }
    catch (err) {
        throw console.log('fetch failed', err);
    }
}

async getAngularContributors() {
    try {
        const response = await 
fetch('https://api.github.com/orgs/angular');
        const got = await response.json();
        await this.props.success(got)
        console.log(this.props.success)
    }
    catch (err) {
        throw console.log('fetch failed', err);
    }
}





render() {


    const angularContributors = this.getAngularContributors
    const angularData = this.props.angularData.data

    console.log(this.props.angularData)
    console.log(angularContributors)

return (
 <AngularIntroduce
     showContributors={angularContributors}
     angularBlog={angularData.blog}
     angularReposCount={angularData.public_repos}
     angularAvatar={angularData.avatar_url}/>

)
}
})

在 props 中将此函数传递给另一个哑组件。有一个

import React from 'react'

 export const AngularIntroduce = (props) =>{
return (
    <div className="container">
       <header>
           <img src={props.angularAvatar} alt=""/>
<h1>Count of Repositories {props.angularReposCount}</h1>
           <h1><a href={props.angularBlog}>Look at their BLOG, there is 
a lot of interesting materials!</a></h1>
<h2 onClick={() => props.showContributors}>Take a look at their contributors!</h2>
//Here i can change it to only props.showContributors that works but i got a error like that fetch failed 
//TypeError: Cannot read property 'props' of undefined
           <HandleClick/>
       </header>
    </div>

)
}

您知道如何获取有关事件的数据吗? 有什么我不知道的吗

【问题讨论】:

标签: javascript reactjs properties ecmascript-6 react-redux


【解决方案1】:

getAngularDatagetAngularContributors 不知道this 是什么意思。您需要做的是将它绑定到 constructor 中,如下所示:

constructor(props) {
    super(this);
    this.getAngularData = this.getAngularData.bind(this);
    this.getAngularContributors = this.getAngularContributors.bind(this);
}

componentDidMount( //... rest of code

【讨论】:

    猜你喜欢
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多