javascript - mapDispatchToProps vs. store.dispatch() - Stack Overflow

Is it more efficient to use mapDispatchToProps inside a ponent to call actions or is it more efficient

Is it more efficient to use mapDispatchToProps inside a ponent to call actions or is it more efficient to use store.dispatch inside action creators?

mapDispatchToProps example:

// Component

import React from 'React';
import { connect } from 'react-redux';
import { defaultAction } from './actions';

class MyComponent extends Component {
  onClickHandler() {
    this.props.dispatch(defaultAction());
  }

  render() {
    return (<div onClick={this.onClickHandler.bind(this)} />);
  }
}

function mapDispatchToProps(dispatch) {
  return { dispatch };
}

export default connect(mapDispatchToProps)(MyComponent);



// Actions

import { DEFAULT_ACTION } from './constants';

export function defaultAction() {
  return {
    type: DEFAULT_ACTION,
  };
}

store.dispatch() example:

// Component

import React from 'React';
import { connect } from 'react-redux';
import { defaultAction } from './actions';

class MyComponent extends Component {
  onClickHandler() {
    defaultAction();
  }

  render() {
    return (<div onClick={this.onClickHandler.bind(this)} />);
  }
}

export default MyComponent;


// Actions

import { DEFAULT_ACTION } from './constants';
import store from './store';

export function defaultAction() {
  store.dispatch({
    type: DEFAULT_ACTION,
  });
}

My guess is that it's more efficient to use mapDispatchToProps instead of importing the entire ./store to dispatch actions.

Is it more efficient to use mapDispatchToProps inside a ponent to call actions or is it more efficient to use store.dispatch inside action creators?

mapDispatchToProps example:

// Component

import React from 'React';
import { connect } from 'react-redux';
import { defaultAction } from './actions';

class MyComponent extends Component {
  onClickHandler() {
    this.props.dispatch(defaultAction());
  }

  render() {
    return (<div onClick={this.onClickHandler.bind(this)} />);
  }
}

function mapDispatchToProps(dispatch) {
  return { dispatch };
}

export default connect(mapDispatchToProps)(MyComponent);



// Actions

import { DEFAULT_ACTION } from './constants';

export function defaultAction() {
  return {
    type: DEFAULT_ACTION,
  };
}

store.dispatch() example:

// Component

import React from 'React';
import { connect } from 'react-redux';
import { defaultAction } from './actions';

class MyComponent extends Component {
  onClickHandler() {
    defaultAction();
  }

  render() {
    return (<div onClick={this.onClickHandler.bind(this)} />);
  }
}

export default MyComponent;


// Actions

import { DEFAULT_ACTION } from './constants';
import store from './store';

export function defaultAction() {
  store.dispatch({
    type: DEFAULT_ACTION,
  });
}

My guess is that it's more efficient to use mapDispatchToProps instead of importing the entire ./store to dispatch actions.

Share Improve this question asked Dec 12, 2016 at 18:18 kelsonickelsonic 1152 silver badges8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

It is generally remended to use dispatch as provided to you via mapDispatchToProps, and not to couple your action creator methods to your store singleton. Here's some in depth discussion about it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745343660a4623445.html

相关推荐

  • javascript - mapDispatchToProps vs. store.dispatch() - Stack Overflow

    Is it more efficient to use mapDispatchToProps inside a ponent to call actions or is it more efficient

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信