React router 如何使用React Router重新锁定当前位置中的状态

React router 如何使用React Router重新锁定当前位置中的状态,react-router,React Router,我很好奇,是否有一个好的方法来替换当前位置的状态。这是我经常做的一种模式,它总是需要从位置创建一个新的位置对象,我觉得必须有更好的方法来实现这一点 import React from 'react'; import withRouter from 'react-router/lib/withRouter'; class Component extends React.Component { updateLocationState(value) { const newLocation

我很好奇,是否有一个好的方法来替换当前位置的状态。这是我经常做的一种模式,它总是需要从位置创建一个新的位置对象,我觉得必须有更好的方法来实现这一点

import React from 'react';
import withRouter from 'react-router/lib/withRouter';

class Component extends React.Component {
  updateLocationState(value) {
    const newLocation = Object.assign({}, this.props.location, {
      state: Object.assign({}, this.props.location.state, {
        myKey: value,
      }),
    });
    this.props.router.replace(newLocation);
  }
  ...
}

export default withRouter(Component);
这感觉非常冗长,它复制了感觉不正确的位置键。有更好的模式吗

我在react路由器v2.6上