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

210
Visualizações
Redux with React-Native and mapStateToProps

I've read this thread. What is the difference between state and props in React?

It says that props are different from states and ideally props should not be change in its component and only should be changed by its parent component.

However, mapStateToProps function in react-redux maps states in Redux to props of a React components, which is basically changing props of a React component when Redux state is changed by an Redux action.

It does not make sense to me. It seems that it should have been mapStateToStates instead and maps Redux states to states of a React component.

Am I missing something?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

It says that props are different from states and ideally props should not be change in its component and only should be changed by its parent component.

State here refers to the internal state of the component, which the component can change internally via .setState().

However, mapStateToProps function in react-redux maps states in Redux to props of a React components, which is basically changing props of a React component when Redux state is changed by an Redux action.

The state here refers to the redux store, an external state. react-redux's connect method creates a HOC - higher order component (a component which is aware of the store's state changes). The HOC wraps the dumb react component, which is not aware of the store. Using mapStateToProps the HOC maps the data from the external state, and inject it to the react component via the props.

State in redux store -> mapStateToProps in HOC -> props passed to the dumb component

So the HOC is the parent, and the dumb component is the child. The parent injects new props to the child component, and the 1st assertion "props should not be change in its component and only should be changed by its parent component" is not violated.


Notes:

  1. More info about higher order component can be found in Dan Abramov's article about Presentational and Container Components.

  2. To understand how react-redux connect works - in the online free course, Getting Started with Redux, Dan Abramov shows how to build connect from scratch (lessons 22-29)

over 4 years ago · Santiago Trujillo Relatório

0

  1. state here in mapStateToProps means state in ReduxStore.

  2. It maps Redux state to ReactComponent props. It's not necessarily to map props to states, e.g. Pure Component

over 4 years ago · Santiago Trujillo Relatório

0

mapStateToProps pass state as a props to component using react-redux connect () function. Consider connect() function as wrapper component, which passes props to children.

                    mapStateToProps
connect (state) ----------------------->  component(props)
over 4 years ago · Santiago Trujillo 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