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

259
Visualizações
React Native LayoutAnimation does not work on android devices

I was trying to create a collapse animationusing LayoutAnimation from React Native and I saw some topics that this was not working only on android devices. To make it possible, it was necessary to put the following code inside a constructor:

if (Platform.OS === 'android') {
      UIManager.setLayoutAnimationEnabledExperimental
        && UIManager.setLayoutAnimationEnabledExperimental(true);
    }

As I'm using functional component, I put this code inside a useEffect without dependecies. However, the animation still not working. Am I doing something wrong or I need to do something more?

My code:

const Collapse = ({ item, onPress }: CollapseProps) => {
  const [layoutHeight, setLayoutHeight] = useState<null | number>(0);

  useEffect(() => {
    if (item.isExpanded) {
      setLayoutHeight(null);
    } else {
      setLayoutHeight(0);
    }
  }, [item.isExpanded]);

  return (
    <View>
      <TouchableOpacity onPress={onPress}>
        <Text>{item.title}</Text>
      </TouchableOpacity>
      <View
        style={{
          overflow: 'hidden',
          height: layoutHeight,
        }}>
        {item.data.map((element: any) => (
          <Text>{element.text}</Text>
        ))}
      </View>
    </View>
  );
};

const Menu = () => {
 const [menuMock, setMenuMock] = useState(mockMenu);

 useEffect(() => {
    if (Platform.OS === 'android') {
      // eslint-disable-next-line no-unused-expressions
      UIManager.setLayoutAnimationEnabledExperimental
        && UIManager.setLayoutAnimationEnabledExperimental(true);
    }
  }, []);

  const updateLayout = (index: number) => {
    LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
    const arrOfMenuMock = [...menuMock] as any;

    arrOfMenuMock[index].isExpanded = !arrOfMenuMock[index].isExpanded;

    // setIsDataOpen(true);
    setMenuMock(arrOfMenuMock);
  };
  
 return (
      <Menu>
        {menuMock.map((item, key) => (
          <Collapse item={item} key={key} onPress={() => updateLayout(key)} />
        ))}
      </Menu>
 )

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

0

Move LayoutAnimation.configureNext before change state

useEffect(() => {
  LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
  if (item.isExpanded) {
    setLayoutHeight(null);
  } else {
    setLayoutHeight(0);
   }
}, [item.isExpanded]);
about 4 years ago · Juan Pablo Isaza Relatório

0

Update: the code should not be in the useEffect, but above the components.

if (Platform.OS === 'android') {
  if (UIManager.setLayoutAnimationEnabledExperimental) {
    UIManager.setLayoutAnimationEnabledExperimental(true);
  }
}

The documentation of LayoutAnimation recommends doing this

https://reactnative.dev/docs/layoutanimation

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