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

234
Visualizações
RN: if one of the prop title lengths is more then, render another background

I want render a card with the second background for both cards if one card has a title > 14 and subtitle > 24. Now it works if the second card changes, but the first is not triggered.

Into the Card component, I am trying to check a length in useEffect and change the state. How is it possible to rework?

Data:

import greenBackground from '@/assets/images/backgrounds/greenBackground.png';
import ligthGreenBackground from '@/assets/images/backgrounds/ligthGreenBackground.png';
import blueBackground from '@/assets/images/backgrounds/blueBackground.png';
import lightBlueBackground from '@/assets/images/backgrounds/lightBlueBackground.png';
import { useTranslation } from 'react-i18next';
const { t } = useTranslation();

const data = useMemo(
  () =>
  ([{
      title: t('partner-title'),
      subTitle: t('partner-subtitle'),
      background: [greenBackground, ligthGreenBackground],
    },
    {
      title: t('subscription.title'),
      subTitle: t('subscription.subtitle'),
      background: [blueBackground, lightBlueBackground],
    },
  ]), [t]);

Card:

const Card = ({background,isChangeBackgound,setIsChangeBackgound,title,subTitle}) => {

  useEffect(() => {
    setIsChangeBackgound(false);
    
    if (title.length > 14 || subTitle?.length > 24) {
      setIsChangeBackgound(true);
    }
  }, [isChangeBackgound]);

  return (
   <ImageBackground style={{width: isChangeBackgound ? 280 : 190 }} source={background}>
     <Text>{title}</Text>
     <Text>{subTitle}</Text>
   </ImageBackground>
  );
};

Component where render card:

const Home = ({ data }) => {
  const [isChangeBackgound, setIsChangeBackgound] = useState(false);

  const renderItem = useCallback(
    ({ item, index }) => {
      return (
        <Card
          background={isChangeBackgound ? item?.background[1] : item?.background[0]}
          isChangeBackgound={isChangeBackgound}
          setIsChangeBackgound={setIsChangeBackgound}
          subTitle={item.subTitle}
          title={item.title}
        />
      );
    },
    [isChangeBackgound]
  );

  const extractKey = useCallback(item => item.title, []);

  return (
      <FlatList
        data={data}
        horizontal
        ItemSeparatorComponent={() => <View style={{ marginRight: 6 }} />}
        keyExtractor={extractKey}
        renderItem={renderItem}
      />
  );
};
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This seems like a lot of extra steps for what you're trying to do. Forgive me if I'm not understanding your intention, but couldn't you handle this all in the Card component?

// Card.js
const Card = ({background, title, subTitle}) => {
  const longTitleOrSubtitle = title.length > 14 || subTitle?.length > 24;

  return (
    <ImageBackground
      style={{width: longTitleOrSubtitle ? 280 : 190 }}
      source={longTitleOrSubtitle ? background[0] : background[1]}
    >
      <Text>{title}</Text>
      <Text>{subTitle}</Text>
    </ImageBackground>
  );
};
// Home.js
const Home = ({ data }) => {
  const renderItem = useCallback(
    ({ item }) => {
      return (
        <Card
          background={item.background}
          subTitle={item.subTitle}
          title={item.title}
        />
      );
    },
    [item]
  );

  const extractKey = useCallback(item => item.title, []);

  return (
      <FlatList
        data={data}
        horizontal
        ItemSeparatorComponent={() => <View style={{ marginRight: 6 }} />}
        keyExtractor={extractKey}
        renderItem={renderItem}
      />
  );
};

You don't need to use state or effects to change the component, especially if the title and subtitle won't change.

about 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