【问题标题】:Error on componentDidMount(), undefined is not a functioncomponentDidMount() 出错,未定义不是函数
【发布时间】:2018-07-12 09:42:12
【问题描述】:

我在 react native 中使用 redux。我正在尝试使用 axios 访问 https://jsonplaceholder.typicode.com
这是代码:

import React, {Component} from 'react'
import {View,Text,StyleSheet} from 'react-native';
import { connect } from 'react-redux';
import {getArticles} from '../Articles';
import {bindActionCreators} from 'redux';

class Articles extends Component{

    componentDidMount(){
        this.props.getArticles()
    }

    render(){
        return(
            <View>
                <Text>Articles</Text>
            </View>
        )
    }
}

function mapStateToProps(state){
    return {
        articles:state.articles
    }
}

function mapDispatchToProps(dispatch){
    return bindActionCreators({getArticles},dispatch);
}

export default connect(mapStateToProps,mapDispatchToProps)(Articles)

这是我想使用 axios 访问该链接的操作:

import axios from 'axios';
const URL = "https://jsonplaceholder.typicode.com"

export function getArticles(){
    const request = axios.get(`${URL}/post`).then(response => response.data)

    return{
        type:"GET_ARTICLES",
        payload: request,
    }
}

这是错误截图:

我是新手,所以也许我犯了一些愚蠢的错误。
谢谢!

【问题讨论】:

    标签: react-native redux react-redux axios


    【解决方案1】:

    试试这个。

    componentDidMount(){
        this.props.dispatch(getArticles())
    }
    

    或将 store 导入组件和

    componentDidMount(){
        store.dispatch(getArticles())
    }
    

    【讨论】:

    • this.props.dispatch(getArticles()),为我工作
    【解决方案2】:

    您为什么要尝试将getArticles 作为道具传递?只需使用:

    componentDidMount(){
        getArticles()
    }
    

    【讨论】:

    • 它仍然显示未定义
    猜你喜欢
    • 2019-10-03
    • 2014-08-03
    • 1970-01-01
    • 2020-11-07
    • 2015-02-24
    • 1970-01-01
    • 2013-06-21
    • 2014-07-14
    • 1970-01-01
    相关资源
    最近更新 更多