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

124
Visualizações
re rendering using ref?

is there anyway I can force re-render a component using ref?

maybe something like this:

const CustomComp=React.memo(React.forwardRef(({...props},ref)=>{
  return(<View ref={ref} style={...}></View>);
}));

export default function App() {
  const Mainref=useRef();

  return(<View>
    <Pressable onPress={()=>{Mainref.TriggerRender()}}></Pressable>
    <CustomComp ref={Mainref}/>
  </View>);

}

that way if there are multiple sibling components with different refs
you wouldn't have to re-render all of them just to re-render one

is such a thing possible? or is there something similar to it?

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

0

You can pass a callback to re-render the child component.

const CustomComp=React.memo(React.forwardRef(({...props},ref)=>{
  const [toggle, setToggle] = React.useState(false);
  function onUpdate() {
    // will trigger re-render
    setToggle(!toggle);
  }
  // set onUpdate on mount
  React.useEffect(() => { 
    if (ref.current) ref.current.onUpdate = onUpdate;
  });
  return(<View ref={ref} style={...}></View>);
}));

export default function App() {
  let Mainref=useRef();

  return(<View>
    <Pressable onPress={()=>{Mainref.current.onUpdate()}}></Pressable>
    <CustomComp ref={Mainref}/>
  </View>);

}

Better approach is to lift the state to a level higher in the component tree, or use a global state manager such as React context API in case the state does not belong in the App component.

const CustomComp=React.memo(React.forwardRef(({...props},ref)=>{
  return(<View ref={ref} style={...}></View>);
}));

export default function App() {
  let Mainref=useRef();
  const [toggle, setToggle] = React.useState(false);
  function onUpdate() {
    // will trigger re-render on the whole view
    setToggle(!toggle);
  }

  return(<View>
    <Pressable onPress={onUpdate}></Pressable>
    <CustomComp ref={Mainref}/>
  </View>);

}
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