Why call a join function without mapStateToProps or mapDispatchToProps as arguments?

In one Redux example from the docs, the connect function is called with no arguments. This can be seen at the bottom of the example.

import React from 'react' import { connect } from 'react-redux' import { addTodo } from '../actions' let AddTodo = ({ dispatch }) => { let input return ( <div> <form onSubmit={e => { e.preventDefault() if (!input.value.trim()) { return } dispatch(addTodo(input.value)) input.value = '' }}> <input ref={node => { input = node }} /> <button type="submit"> Add Todo </button> </form> </div> ) } AddTodo = connect()(AddTodo) export default AddTodo 

In my understanding, the purpose of the connect function is to provide container components with access to action dispatchers in the form of callbacks, as well as access to the state from the repository in the form of details.

Therefore, it makes no sense why you call connect without specifying the creators of the state and actions to which you want to grant access to the component.

+5
source share
1 answer

connect()(AddTodo) will pass dispatch as a reference to the AddTodo component, which is still useful even without state or predefined actions.

Redux (and reaction-reduction) is a pretty low-level library. This allows you to be very strict about what state and actions the component has, or you can transfer the entire store and each action to each component and everything in between.

It's up to you what level of rigor is right for your application.

Why use connect at all, you may ask? A good connect just passes the repository / send through the React context, so you don't need to pass the repository through many components. However, you do not need to use connect . Any module / HOC template can work, connect easy to use.

+5
source

Source: https://habr.com/ru/post/1261003/


All Articles