[英]React-redux Getting error while rendering post in TypeError:Cannot read property 'map' of undefined but console having all posts
As I am new to react ,I am trying to make a simple blog app using redux with react but I am facing issue while fetching the blogs.It is giving error TypeError: Cannot read property 'map' of undefined .Here is my code so please help me I m stuck here.由于我刚开始反应,我正在尝试使用 redux 和 react 来制作一个简单的博客应用程序,但是我在获取博客时遇到了问题。它给出了错误TypeError: Cannot read property 'map' of undefined 。这是我的代码请帮助我,我被困在这里。
actions/index.js动作/ index.js
export const FETCH_POSTS = 'fetch_posts';
export function fetchPosts() {
const request = axios.get(`${ROOT_URL}/posts${API_KEY}`);
return {
type: FETCH_POSTS,
payload: request
};
}
reducers/reducer_posts.js减速器/减速器_posts.js
import { FETCH_POSTS } from '../action';
export default function (state = {post:[]}, action) {
switch (action.type) {
case FETCH_POSTS:
return {
...state,
post: action.payload,
}
default:
return state;
}
}
reducers/index.js减速器/ index.js
import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';
import PostsReducer from './reducer_posts';
const rootReducer = combineReducers({
posts: PostsReducer,
form: formReducer
});
export default rootReducer;
component/posts_index.js组件/posts_index.js
class PostsIndex extends Component {
componentDidMount() {
this.props.fetchPosts();
}
render() {
const post1=this.props
console.log(post1) //getting data in console
const map_post= this.post1.map(post => <li key={post.id}>{post.title}</li>)
console.log(this.props.posts); // getting data here as well
return(
<div>
<ul className="list-group">
{map_post}
</ul>
</div>
);
}
}
function mapStateToProps(state) {
return { posts: state.posts ,
post1:state.posts.posts
};
}
export default connect(mapStateToProps, { fetchPosts })(PostsIndex);
src/App.js源代码/App.js
const createStoreWithMiddleware = applyMiddleware(promise)(createStore);
function App() {
return (
<Provider store={createStoreWithMiddleware(reducers)}>
<BrowserRouter>
<div className="App">
<Switch>
<Route path="/posts/new" component={PostsNew} />
<Route path="/posts/:id" component={PostsShow} />
<Route path="/" component={PostsIndex} />
</Switch>
</div>
</BrowserRouter>
</Provider>
);
}
Also I am getting all the post in my console when I log it by commenting out the此外,当我通过注释掉
const map_post= post1.map(post => <li key={post.id}>{post.title}</li>)
but I am not able to render it in my component.I am getting error × TypeError: Cannot read property 'map' of undefined但我无法在我的组件中呈现它。我收到错误× TypeError:无法读取未定义的属性“地图”
Console Output having All post具有所有帖子的控制台输出
Thanks in advance提前致谢
Try changing your method尝试改变你的方法
componentDidMount() {
this.props.fetchPosts();
}
instead of代替
componentDidMount() {
this.props.dispatch(fetchPosts());
}
Also in render method add this.post to同样在渲染方法中添加 this.post
const mappedTweets = this.post1 && this.post1.map(post => <li key={post.id}>{post.title}</li>);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.