代码之家  ›  专栏  ›  技术社区  ›  Gerardo Bautista

反应并重新定义this.props.posts未定义?

  •  0
  • Gerardo Bautista  · 技术社区  · 7 年前

    我试着跟随一个教程,比如一个带有react和redux的博客,但是在 PostList 我在reducer的绑定上有错误,我只想呈现函数 renderPost 使用 map 属于 posts

    我的代码中有什么错误?

    这是我的组件 邮政名单 :

    import React from 'react';
    import {connect } from 'react-redux';
    import {cargaPost} from '../acciones/index';
    import {Link} from 'react-router-dom';
    class ListaPost extends React.Component {
        componentWillMount() {
            this.props.fetchPost();
        }
        renderPost () {
            return this.props.posts.map( post => {
                <li className="list-group-item" key={post.id}>
                    <span className="text-right">{post.categories}</span>
                </li>
            })
        }
        render() { 
            // const { posts } = this.props;
            // if (!posts) {
            //   return <div>Loading...</div>
            // }
            return (
                <div>
                    <div className="text-right">
                        <Link
                        to="/posts/new" 
                        >
                        <button 
                        type="button" 
                        className="btn btn-info">
                        Nuevo Post
                        </button>
    
                        </Link>
                    </div>
    
                    <ul className="list-group">
                        {this.renderPost()}
                    </ul>
                </div>
            )
        }
    }
    function mapStateToProps(state) {
      return { posts: state.todos }
    }
    export default connect(mapStateToProps, {fetchPost: cargaPost})(ListaPost);
    

    还有我的 减速柱

    import {FETCH_POST} from '../acciones/index';
    
    const estadoInicial = { todos:[], unPost: null }
    // PostReducer
    export default function(state=estadoInicial, accion) {
        switch(accion.type) {
            case FETCH_POST:
                return {...state, todos: accion.payload.data }
            default:
                return state;
        }
    }
    

    我的 指数 文件

    import {combineReducers} from 'redux';
    import PostReducer from './PostReducer';
    
    const rootReducer = combineReducers({
        posts: PostReducer
    });
    
    export default rootReducer;
    

    enter image description here

    3 回复  |  直到 7 年前
        1
  •  4
  •   Hriday Modi Victor Manuel    7 年前

    在你 mapStateToProps 而不是 state.todos 应该是 state.posts.todos . 见以下代码:

    function mapStateToProps(state) {
        return { posts: state.posts.todos }
    }
    

    你的根还原程序将返回 state 有钥匙的商店 posts 在它里面(这是为所有的减速器,你要组合成根减速器),和 帖子 你有你的 todos .

        2
  •  3
  •   Colin Ricardo    7 年前

    我想这个,在你的 mapStateToProps ,

    return { posts: state.todos }
    

    应该是

    return { posts: state.posts }
    

    或者,如果你只想得到todo,那么:

    return { posts: state.posts.todos }
    
        3
  •  0
  •   Gerardo Bautista    7 年前

    enter image description here

    并修复代码,但我仍然显示错误,请检查我的repo https://github.com/emeery/blog

        4
  •  0
  •   Digital Atul    5 年前

    在mapstatetrops中使用此代码

    const mapStateToProps = (state) => {
        return { posts: state.posts }
    }