swelet
swelet

Reputation: 8702

Why separate methods for mapDispatchToProps and mapStateToProps?

I'm writing a redux app and I ran in to a small but nagging question. Why is the convention to separate the mapDispatchToProps and mapStateToProps methods? Why not just have one:

mapToProps = (state, dispatch) => {
    ...
}

Upvotes: 5

Views: 247

Answers (2)

Amit Patil
Amit Patil

Reputation: 3067

As mentioned in below link

The separation exists for performance reasons: mapStateToProps and mapDispatchToProps are separate for a good reason, consider the performance: mapStateToProps is actually run several times when state changes, and mapDispatchToProps once (or way fewer anyway than mapStateToProps) it doesn't depend on the state.

Discussion

Upvotes: 0

Simon Boudrias
Simon Boudrias

Reputation: 44609

Because the mapDispatchToProps doesn't need to be recompiled when the state change (it's only called once).

Upvotes: 6

Related Questions