从本地存储和URL哈希重新水化状态

时间:2017-06-27 16:15:08

标签: javascript reactjs url redux local-storage

我已经编写了以下减速器并且它可以工作,但我觉得我正在进入变异领域并且可能有更好的方法来实现这一点。我在州里有一个叫做阅读的对象。可以从URL中的散列中提取读数,也可以从存储在读取对象内的称为oldReadings的读数记录中提取读数。第三种方法是从本地存储中的持久状态中提取它。我希望URL提取优先,以便轻松共享。这种做法是否正常,如果没有,你能指出我正确的方向吗?

import { GENERATE_READING, GET_READING_FROM_HASH } from '../constants/actionTypes';
import { REHYDRATE } from 'redux-persist/constants';
import getHashFromUrl from '../utils/getHashFromUrl';
import getReadingFromHash from '../utils/getReadingFromHash';

// Check if URL has hash and set initial state to that reading.
const initialUrlHash = getHashFromUrl(window.location.pathname);
const initialUrlReading = initialUrlHash ? getReadingFromHash(initialUrlHash) : null;
const initialState = {
  arr: initialUrlReading ? initialUrlReading.arr : [],
  hash: initialUrlHash ? initialUrlHash : '',
  time: 0,
  oldReadings: []
};

export const reading = (state = initialState, action) => {
  switch (action.type) {
    case GENERATE_READING: {
      let reading = action.reading;
      // If old readings exists, save new reading to array of old readings.
      if (state.oldReadings) {
        reading.oldReadings = [ {
          hash: reading.hash,
          time: reading.time,
          amount: reading.arr.length
        }, ...state.oldReadings ];
        reading.oldReadings = reading.oldReadings.slice(0, 5);
      }
      // Otherwise create new array and save reading to it.
      else {
        reading.oldReadings = [{
          hash: reading.hash,
          time: reading.time,
          amount: reading.arr.length
        }];
      }

      return { ...state, ...reading };
    }

    case GET_READING_FROM_HASH:
      // Used when restoring reading for old hash.
      return { ...state, ...action.reading };

    case REHYDRATE: {
      const incoming = action.payload.reading;
      // If hydrating from local storage, check for hash in url and append
      // that reading on top of reading from local storage.
      if (incoming) {
        const urlHash = getHashFromUrl(window.location.pathname);
        if (urlHash && incoming.hash !== urlHash) {
          const urlReading = getReadingFromHash(urlHash);
          return { ...state, ...incoming, ...urlReading };
        } else {
          return { ...state, ...incoming };
        }
      } else {
      return state;
      }
    }

    default:
      return state;
  }
};

export default reading;

1 个答案:

答案 0 :(得分:0)

我认为您可以使用Object.assign来避免突变并简化您的代码。例如,而不是let reading = action.reading,它仍然只是对原始对象的引用,您可以const reading = Object.assign({}, action.reading)。这基本上需要一个新的空对象并将action.reading复制到其中。看看文档,Object.assign对于这类事物是非常有价值的。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign