Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New features discussion #33

Open
ben196888 opened this issue Jan 2, 2017 · 3 comments
Open

New features discussion #33

ben196888 opened this issue Jan 2, 2017 · 3 comments
Assignees
Labels

Comments

@ben196888
Copy link
Contributor

ben196888 commented Jan 2, 2017

我們還有未來嗎?(眼神死

@ben196888
Copy link
Contributor Author

A few quick idea of the technique we might use.

  • Immutable JS
  • Normalize
  • Apollo

@ElaineHuang
Copy link
Contributor

@ben196888
Copy link
Contributor Author

ben196888 commented Apr 4, 2017

Let me show you a brilliant method to seperate the state logic part and view rendering part works. And you never ever worry about the immutable would interrupt ES6 styles.

We create selector function (or helper function as well) for the reducer. For example:

import { Map } from 'immutable';
import { getNextGoodId } from '../API';

const EMPTY_MAP = new Map();
const EMPTY_OBJECT = Object.create({});

export const getOwnerIdByGoodId = (state, goodId) => {
  return state.get(goodId).getIn(['owner', 'id']).toJS() || EMPTY_OBJECT;
};

const goodReducer = (state = EMPTY_MAP, action) => {
  switch (action.type) {
  case 'ADD_GOOD': {
    const goodId = getNextGoodId();
    return state.set(goodId, action.good);
  }
  default:
    return state;
  }
};

export default goodReducer;

As a result, we could still get the state easily in container. Such as

import { connect } from 'react-redux';
import { getStore, getGoodById } from '../store';
import component from './component';

const mapStateToProps = (state, props) => {
  const { goodId } = props;
  const state = getStore().getState();
  const ownerId = getOwnerIdByGoodId(state, goodId);
  return {
    ownerId,
  }
};

export default container = connect(mapStateToProps)(component);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants