Skip to content

Instantly share code, notes, and snippets.

@athulmurali
Last active June 1, 2019 03:39
Show Gist options
  • Save athulmurali/8b6ec016d4f63d108f56e66679d5088f to your computer and use it in GitHub Desktop.
Save athulmurali/8b6ec016d4f63d108f56e66679d5088f to your computer and use it in GitHub Desktop.
Sample redux code
import React from 'react';
import WidgetListContainer from './WidgetListContainer'
import {applyMiddleware, createStore} from 'redux'
import logger from 'redux-logger'
import {Provider} from 'react-redux'
import {widgetReducer} from "../reducers/widgetReducer"
let store = createStore(widgetReducer);
export default class WidgetListEditor
extends React.Component {
constructor(props) {
super(props);
this.state = {
courseId: '',
moduleId: '',
lessonId: '',
topicId: '',
topic: {}
};
this.setCourseId = this.setCourseId.bind(this);
this.setModuleId = this.setModuleId.bind(this);
this.setLessonId = this.setLessonId.bind(this);
this.setTopicId = this.setTopicId.bind(this);
}
componentDidMount() {
this.setCourseId(this.props.courseId);
this.setModuleId(this.props.moduleId);
this.setLessonId(this.props.lessonId);
this.setTopicId(this.props.topicId);
}
componentWillReceiveProps(newProps) {
this.setCourseId(newProps.courseId);
this.setModuleId(newProps.moduleId);
this.setLessonId(newProps.lessonId);
this.setTopicId(newProps.topicId);
}
setTopicId(topicId) {
this.setState({topicId: topicId});
}
setLessonId(lessonId) {
this.setState({lessonId: lessonId});
}
setModuleId(moduleId) {
this.setState({moduleId: moduleId});
}
setCourseId(courseId) {
this.setState({courseId: courseId});
}
render() {
return(
<Provider store={store}>
<WidgetListContainer topicId={this.state.topicId}/>
</Provider>
);}}
import * as constants from "../../constants/index"
import WidgetService from "../../services/WidgetService";
initialState = {widgets: [], preview: false};
export const widgetReducer = (state = initialState, action) => {
const widgetServiceInstance = WidgetService.instance
switch (action.type) {
case constants.PREVIEW:
return {
widgets: state.widgets,
preview: !state.preview
}
// all other cases here
default:
return state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment