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

77
Visualizações
How to add View in react native during runtime?

I'm really confused on how to add (and delete) View and other such components during runtime,
for example in vanilla JavaScript you can use document.querySelector('query-here').appendChild(element);

but how do I achieve the same thing using react native? for example:

<Pressable onPress={()=>{addElement(element)}}>
<View>
 //add elements here
</View>

I know how to achieve it directly like this:

<View>
 {
  [...Array(23)].map((el, index) => {
    return(
     <View key={index}>
        <Text>added new element</Text>
     </View>
   )});
 }
</View>

could someone please point me in the right direction?

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

0

@cakelover here how you can add item and remove items based on component's state.

import { Button } from 'react-native';


const [loader, setLoader] = React.useState(false); //donot show loader at initial
    const showLoader = isShowLoader => { // based on this function you can add or remove single loader from UI
       setLoader(isShowLoader);
    }
    return (
       <View>
         {loader && <LoaderComponent/>}
         <Button
           onPress={() => setLoader(!loader)}
           title="Toggle Loader Component"
           color="#841584"
         />
       </View>
     )

If you want to add or remove multiple same components like list you should use arrays of items for that.

about 4 years ago · Juan Pablo Isaza Relatório

0

I'm not sure but maybe you could try something like this

  export default function App() {

  const [num, setNum] = useState(() => 0);
  const [renderTasks, setRenderTasks] = useState(()=>taskcreate(0));


  function taskcreate()
  {
    let i=num;
    setNum(i+1);
    return(
      <View>
      {
        [...Array(i)].map((el, index) => {

          return (
            <View key={index}>
              <Text>hello there</Text>
            </View>
          )
        })
      }
      </View>
    )

  }

  return (
    <View style={styles.container}>
      <Pressable style={{ height: 50, width: 50, backgroundColor: 'orange' }} onPress={() => { setRenderTasks(taskcreate()) }}></Pressable>
      { renderTasks }
    </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