Skip to content

Instantly share code, notes, and snippets.

import { connect } from 'react-redux';
import PhotoList from 'components/PhotoList';
import { fetchPhotos } from './modules/Photos/actions';
const mapStateToProps = state => ({
photos: state.photos.photos,
});
const mapDispatchToProps = {
import { ADD_PHOTOS } from './actions';
function getInitialState() {
return {
photos: [],
};
}
export default function reducer(state = getInitialState(), action) {
switch (action.type) {
import api from './api';
export const ADD_PHOTOS = 'Photos/ADD_PHOTOS';
export function addPhotos(photos) {
return {
type: ADD_PHOTOS,
payload: photos,
};
}
@tfiechowski
tfiechowski / cache-child-state.js
Created April 6, 2018 09:18
Cache child component state
class Child extends Component {
state = this.props.cachedState
? this.props.cachedState
: {
value: 0,
};
componentWillUnmount() {
if (this.props.saveState) {
this.props.saveState(this.state);
@tfiechowski
tfiechowski / rootReducer.js
Last active April 2, 2018 09:52
WIP: Redux root reducer caching state in local storage
import { CHANGE_TEAM } from 'modules/Team/Team';
const getCurrentTeamId = () => window.localStorage.getItem('current-team');
const readTeamData = teamId => {
const newStateData = window.localStorage.getItem(`team-data-${teamId}`);
if (newStateData) {
return JSON.parse(newStateData);
}