Object.assign concat - ES6重构

时间:2018-03-16 18:01:28

标签: javascript reactjs ecmascript-6 redux react-redux

如何使用ES6重构此redux reducer代码?康卡特我可以用...代替吗?但是Object.assign呢?

import { FETCH_REQUEST, FETCH_SUCCESS, FETCH_FAILURE } from '../actions';

const initialState = {
  isFetching: false,
  beersArray: [],
  errorMessage: '',
  skip: 0,
  limit: 0,
  hasMore: true,
};

export default function(state = initialState, action) {
  switch (action.type) {
    case FETCH_REQUEST:
      return Object.assign({}, state, {
        isFetching: true,
        skip: action.skip,
        limit: action.limit,
        hasMore: true,
      });
    case FETCH_SUCCESS:
      return Object.assign({}, state, {
        isFetching: false,
        beersArray: state.beersArray.concat(action.beers),
        hasMore: action.hasMore,
      });
    case FETCH_FAILURE:
      return Object.assign({}, state, {
        isFetching: false,
        errorMessage: action.errorMessage,
        hasMore: false,
      });
    default:
      return state;
  }
}

2 个答案:

答案 0 :(得分:2)

您可以将Object.assign替换为。请注意,...state(点差属性)是提案,因此您的环境需要支持它们。

return { ...state, isFetching: true, skip: action.skip, limit: action.limit, hasMore: true };

concat怎么样,你可以写

{ beersArray: [...state.beersArray, ...action.beers] }

这只会破坏state.beersArrayaction.beers中的项目,将它们放入新数组并分配给beersArray

答案 1 :(得分:0)

您也可以在public class AspectFitViewController : UIViewController { // "native" size for the holderView let hViewWidth: CGFloat = 700.0 let hViewHeight: CGFloat = 200.0 let topView: UIView = { let v = UIView() v.translatesAutoresizingMaskIntoConstraints = false v.backgroundColor = UIColor.blue return v }() let holderView: UIView = { let v = UIView() v.translatesAutoresizingMaskIntoConstraints = false v.backgroundColor = UIColor.cyan return v }() public override func viewDidLoad() { super.viewDidLoad() view.bounds = CGRect(x: 0, y: 0, width: 400, height: 600) view.backgroundColor = .yellow // add topView view.addSubview(topView) // pin topView to leading / top / trailing topView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0.0).isActive = true topView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0).isActive = true topView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0).isActive = true // explicit height for topView topView.heightAnchor.constraint(equalToConstant: 250.0).isActive = true // add holderView to topView topView.addSubview(holderView) // center X and Y holderView.centerXAnchor.constraint(equalTo: topView.centerXAnchor, constant: 0.0).isActive = true holderView.centerYAnchor.constraint(equalTo: topView.centerYAnchor, constant: 0.0).isActive = true // aspect ratio size holderView.widthAnchor.constraint(equalTo: holderView.heightAnchor, multiplier: hViewWidth / hViewHeight).isActive = true // two constraints for each side... // the .equal constraints need .defaultLow priority // top and leading constraints must be .greaterThanOrEqual to 0 // bottom and trailing constraints must be .lessThanOrEqual to 0 let topA = NSLayoutConstraint(item: holderView, attribute: .top, relatedBy: .greaterThanOrEqual, toItem: topView, attribute: .top, multiplier: 1.0, constant: 0.0) let topB = NSLayoutConstraint(item: holderView, attribute: .top, relatedBy: .equal, toItem: topView, attribute: .top, multiplier: 1.0, constant: 0.0) let bottomA = NSLayoutConstraint(item: holderView, attribute: .bottom, relatedBy: .lessThanOrEqual, toItem: topView, attribute: .bottom, multiplier: 1.0, constant: 0.0) let bottomB = NSLayoutConstraint(item: holderView, attribute: .bottom, relatedBy: .equal, toItem: topView, attribute: .bottom, multiplier: 1.0, constant: 0.0) let leadingA = NSLayoutConstraint(item: holderView, attribute: .leading, relatedBy: .greaterThanOrEqual, toItem: topView, attribute: .leading, multiplier: 1.0, constant: 0.0) let leadingB = NSLayoutConstraint(item: holderView, attribute: .leading, relatedBy: .equal, toItem: topView, attribute: .leading, multiplier: 1.0, constant: 0.0) let trailingA = NSLayoutConstraint(item: holderView, attribute: .trailing, relatedBy: .lessThanOrEqual, toItem: topView, attribute: .trailing, multiplier: 1.0, constant: 0.0) let trailingB = NSLayoutConstraint(item: holderView, attribute: .trailing, relatedBy: .equal, toItem: topView, attribute: .trailing, multiplier: 1.0, constant: 0.0) topB.priority = .defaultLow bottomB.priority = .defaultLow leadingB.priority = .defaultLow trailingB.priority = .defaultLow NSLayoutConstraint.activate([ topA, topB, bottomA, bottomB, leadingA, leadingB, trailingA, trailingB ]) } } 个对象和key-value上使用Spread运算符。

arrays

转换

export default function(state = initialState, action) { switch (action.type) { case FETCH_REQUEST: return {...state, { isFetching: true, skip: action.skip, limit: action.limit, hasMore: true}}; case FETCH_SUCCESS: return {...state, { isFetching: false, beersArray: [...state.beersArray, ...action.beers], hasMore: action.hasMore }); case FETCH_FAILURE: return { ...state, { isFetching: false, errorMessage: action.errorMessage, hasMore: false }); default: return state; } } state.beersArray.concat(action.beers)

[...state.beersArray, ...action.beers]Object.assign({}, state, {other props}}

Spread syntax (...)

  

Spread syntax允许在需要零个或多个参数(用于函数调用)或元素(用于数组文字)的位置扩展数组表达式或字符串等可迭代的对象,或者扩展对象表达式在需要零个或多个键值对(对象文字)的地方。