Skip to content

Instantly share code, notes, and snippets.

@ghoshabhi
Created August 11, 2017 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghoshabhi/8af3b059ae4ecdb333783a7da4c01b26 to your computer and use it in GitHub Desktop.
Save ghoshabhi/8af3b059ae4ecdb333783a7da4c01b26 to your computer and use it in GitHub Desktop.
export const fetchByPostId = id => {
const data = API.getPostById(id);
return {
type: FETCH_POST_BY_ID,
payload: data,
}
}
import React, { Component } from 'react';
import { connect, bindActionCreators } from 'react-redux';
import * as actions from '../some/actionCreatorFile.js';
class FooContainer extends Component {
componentDidMount() {
this.props.fetchByPostId(this.props.match.params.id);
}
render() {
return (
<SomeStateLessComponent post={this.props.postFoo} />
)
}
}
// State => Props
mapStateToProps = (state) => {
return {
postFoo: state.postConatiner.post,
}
}
export default connect(mapStateToProps, { actions })(FooContainer);
//import types (FETCH_POST_BY_ID)
const initialState = {
post: {},
}
const postReducer = (state = initialState, action) => {
switch(action.type) {
case FETCH_POST_BY_ID: {
return {
...state,
post: action.payload,
}
}
default:
return state;
}
}
export default initialState;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment