Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!