无法从actionCreator取回数据

时间:2019-04-10 06:18:12

标签: reactjs redux redux-thunk

最近我一直在redux上工作,并且试图让自己动手,我尝试通过redux-thunk进行异步调用。
我正在显示relevent文件。
Posts.js
该文件在componentdidmount中调用getPosts(),该方法试图从actionCreator中获取数据。

 import React,{Component} from 'react'
import Axios from 'axios'
import CardPost from '../Post/Post'
import Container from 'react-bootstrap/Container'
import Row from 'react-bootstrap/Row'
import Col from 'react-bootstrap/Col'
import classes from './Posts.module.css'
import {Link,Route} from 'react-router-dom'
import {connect} from 'react-redux'
import {getPost} from '../../Store/action/posts'
class Posts extends Component{


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

    render(){
        let post =<p>Loading data...</p>
       if(this.props.bulkPost.length>0){
           console.log("rendering again")
            post =  this.props.bulkPost.map((item)=>{
            return(

    <Col as={Link} to={"/posts/"+item.id} className={classes.topMargin} key={item.id} sm={4}> 
        <CardPost/>
    </Col>

            )

           })
       }
        return(
            <>
            <div>
                 <Container>
                        <Row> 
                            {post}
                        </Row>
               </Container>
            </div>

        </>
        )
    }
}

const mapStateToProps = state=>{
    return{ 
        bulkPost: state.pos.posts,
        // oneLineData: state.full.fetchedText
    }
}

const mapDispatchToProps = dispatch =>{
    return{

        // onDeleteResult: (id)=>dispatch(actionCreator.deleteResult(id)),
        getPosts:()=>dispatch(getPost())
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(Posts)  

这是我的actionCreator posts.js
它利用redux thunk中间件并尝试获取数据异步,通过console.log(res)检查时,正在文件中提取数据。

    import {POSTS} from '../action/actionType'
import Axios from 'axios'

export const savePost = (res)=>{
    return{
        posts: res,
        type:POSTS

    }
}

export const getPost = ()=>{
    return (dispatch)=>{
        Axios.get("https://jsonplaceholder.typicode.com/posts/").then((response)=>{    
        dispatch(savePost(response.data));
        })
    }
}

现在是理想情况下应该分发数据的第三个文件,应该将其返回给我们

    import * as actionType from '../action/actionType'
const intitalState = {
    posts:[]
}

const reducer = (state=intitalState, action)=>{
    switch(action.type){
        case actionType.POSTS:
                console.log(state)
                const post  = state.posts.slice(0,4);
                const updatedPost = post.map((post)=>{
                    return{
                    ...post
                    }
                })
                    return{
                        ...state,
                        posts:updatedPost
                    }
    }
    return state
}

export default reducer

在这里,状态文件未通过第一个文件中的dispatch方法填充。 有人可以帮忙,我在这里做错了什么吗?
编辑: 这是完整的在线工作项目
https://codesandbox.io/s/4xwlwl70v0

1 个答案:

答案 0 :(得分:0)

在动作创建者中,您可以直接分派动作:

import Axios from 'axios'
import {POSTS} from '../action/actionType'

export const getPost = ()=>{
    return (dispatch)=>{
        Axios.get("https://jsonplaceholder.typicode.com/posts/").then((response)=>{    
            dispatch({
                type: POSTS,
                posts: response.data
            });
        })
    }
}

在化简器中,您正在映射API调用的结果。我看不出映射结果的目的吗?来自动作创建者的帖子可以在action.posts上找到。

import * as actionType from '../action/actionType'
const initalState = {
    posts: []
}

const reducer = (state = initalState, action)=>{
    switch(action.type){
        case actionType.POSTS:
            const posts = action.posts.slice(0,4);
            return{
                ...state,
                posts: posts
            }
    }
    return state
}

export default reducer;

没有看到你的联合履历表,这是错字吗?

const mapStateToProps = state=>{
    return{ 
        bulkPost: state.pos.posts, // maybe state.post.posts ?
        // oneLineData: state.full.fetchedText
    }
}