Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
How do you achieve the below code in react-native when user presses a button

This is how we dynamically add elements in vanilla java script,how could this be achieved in react-native.

  let Message = '<div class="text-right " style="padding-bottom:5px;border-radius:25px;width:100%">' +
                '<div class="pl-5"  style="background:#f1f0e8 ;border-radius:25px;width:100%;">' +
                '<p class="pr-2 message" style="border-radius:25px;background-color:rgb(192, 192, 192);width:100%">' +
                $scope.message + 
                '<small>sent now</small>' +
                '</p>' +
                '</div>' +
                '</div>';

            $('.chat-box').append(Message)
            $scope.message = "";
            
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

The logic for this remains the same in both react and react-native. You need a state that contains the list of contents and must map that state to react components. On Button press, you have to append another content item. Unlike vanilla js you shouldn't try storing components as string, rather use like the below example

import React, { useState } from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';

export default function App() {
  // The state containing the content array which is set default to an empty array
  const [messages, setMessages] = useState([]);
  return (
    <View style={styles.container}>
      <View>
        {/* Map the state to a component list */}
        {messages.map((msg, index) => (
          <Text key={'msg-' + index}>Random Number Added - {msg.message}</Text>
        ))}
      </View>
      <View style={{ flexDirection: 'row',justifyContent: 'space-between' }}>
        <Button
          // Append a new message
          onPress={() => setMessages((m) => [...m, { message: Math.random() }])}
          title="Add"
        />
        <Button onPress={() => setMessages([])} title="Clear" />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'space-between',
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});

Live Demo of the code: https://snack.expo.dev/@arnabxd/stackoverflow-72606009

You need to have a basic understanding of states in react.

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda