Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

230
Visualizações
How to maximize composability with React higher-order functions?

I refer to this part of the official React tutorial (Convention: Maximizing Composability):

// connect is a function that returns another function
const enhance = connect(commentListSelector, commentListActions);
// The returned function is a HOC, which returns a component that is connected
// to the Redux store
const ConnectedComment = enhance(CommentList);

How should we implement the connect() function?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Conceptually HOC connect is something like this:

function connect(mapStateToProps, mapDispatchToProps) {
  return function (WrappedComponent) {
    return class extends React.Component {
      render() {
        return (
          <WrappedComponent
            {...this.props}
          />
        )
      }
    }
  }
}

How you can see, it's a function that return another function.

At first call connect get 2 arguments

const enhance = connect(commentListSelector, commentListActions);

At the second it get Component which we return in connect function

const ConnectedComment = enhance(CommentList);

But not only component, we return connect with new props which we get from redux - so it's approach how to get data from redux, look at more detailed example (if it's not enough for you, you can check link in a comment above):

function connect(mapStateToProps, mapDispatchToProps) {
  return function (WrappedComponent) {
    return class extends React.Component {
      render() {
        return (
          <WrappedComponent
            {...this.props}
            {...mapStateToProps(store.getState(), this.props)}
            {...mapDispatchToProps(store.dispatch, this.props)}
          />
        )
      }
    } 
  } 
}

For me more useful and convenient is react-hook pattern, check useSelector and useDispatch methods to get data from redux component to react. It's simplier, connect a legacy approach and can be useful only with Class components.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda