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

99
Visualizações
React Native Async Storage setItem call fails

I'm writing a simple Async Storage set Item call in React Native to store my store's cart data. But the call fails. I can't find possible reason why it so as there is no mention of such in the documentation. I have added the relevant code. Please help would be appreciated.

const ProductDetailScreen = (props) => {
  const {product} = props.route.params;

  const getCart = async () => {
    return await AsyncStorage.getItem('cart');
  };

  const cart = getCart();

  const addToCart = async () => {
    try {
      await AsyncStorage.setItem('cart', [
        ...cart, JSON.stringify(product),
      ]);
    } catch (e) {
      console.error(`Failed to add item: {${JSON.stringify(product)}} to cart`);
    }
  };

  return (
    <>
      <TouchableOpacity onPress={addToCart}>
          <Button>
            <Text style={styles.btnText}>Add to Cart</Text>
          </Button>
        </TouchableOpacity>
    </>
  )
};

export default ProductDetailScreen;

error log

ERROR  Failed to add item: {{"id":2,"name":"Scene Stealers","description":"Corset cuts","price":250,"image":5,"colors":["#0F140D","#DD8560","#E1E0DB"],"sizes":["S","M","L"]}} to cart
almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can only store/set string data in AsyncStorage while you are trying to store Array

enter image description here

await AsyncStorage.setItem('cart', JSON.stringify([...cart, JSON.stringify(product)]))
almost 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