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

392
Visualizações
React ref returns a 'Connect' object instead of DOM

I'm trying to create dynamics refs for custom components created through the map function.

class PostsList extends Component {

  constructor(props) {
    super(props);
  }

  componentDidUpdate = () => {
    console.log(this.refs);
  }

  render() {
    let posts = this.props.posts || [];
    return (
        <div ref="test">
          {posts.map((post) => {
            return <Post post={post} key={post.id} ref={post.id}></Post>
          })}
        </div>
    );
  }

}

export default PostsList

the console.log returns the correct DOM node for refs.test but for the ones in the loop, it's returning a Connect Object. Screenshot

Can someone point me in the right direction?

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

0

It seems like Post is a connected component, while you actually want the wrapped one.

react-redux ≥ 6.0.0

Connect with forwardRef: true

connect(null, null, null, { forwardRef: true })(Post);

then add a ref normally:

ref={ref => this.<id> = ref}

From the docs:

If {forwardRef : true} has been passed to connect, adding a ref to the connected wrapper component will actually return the instance of the wrapped component.




react-redux < 6

Connect with withRef: true

connect(null, null, null, { withRef: true })(Post);

then use getWrappedInstance() to get the underlying connected component:

this.refs[<id>].getWrappedInstance()

From the docs:

[withRef] (Boolean): If true, stores a ref to the wrapped component instance and makes it available via getWrappedInstance() method. Default value: false

over 4 years ago · Santiago Trujillo Relatório

0

An alternative way to do this would be to use some other prop name (other than ref). For example:

<Post
  ...
  innerRef={(node) => { this.myRef = node; }}
/>

This also works if you're using a library like styled-components or emotion

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