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

250
Views
How to use a window prompt as a currency reactjs

When I run this code it will just repeat doing window prompt "what currency do you use?", even once I enter a valid currency. I'm very new to js and react so sorry if this is really simple

import React from "react"

function Product(props) {
    const MoreInfo = () => {
        alert(props.product.description);
    }
    const UserCurrency = window.prompt("What currency do you use?")
    
    var formatter = new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: UserCurrency,
    })
    
    return (
        <div>
            <h2>{props.product.name}</h2>
            <p>{formatter.format(props.product.price)}</p>
            <button onClick={MoreInfo}>More Info</button>
        </div>
    )
}

export default Product
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to learn about react hooks, in this case: useState and useEffect

import { useState, useEffect } from 'react'

function Product(props) {
    
    const [userCurrency, setUserCurrency] = useState(null)
    useEffect(() => {
      const uc = window.prompt("What currency do you use?")
      setUserCurrency(uc)
    }, [])

 
    const MoreInfo = () => {
        alert(props.product.description);
    }
    
    var formatter = new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: UserCurrency,
    })
    
    return (
        <div>
            <h2>{props.product.name}</h2>
            <p>{formatter.format(props.product.price)}</p>
            <button onClick={MoreInfo}>More Info</button>
        </div>
    )
}

export default Product

with useEffect, the code will run only once when the component is mounted, if you pass an empty array of dependencies as a second argument.

Please read the docs, as this explanation is far from enough to understand what's at stake here

https://reactjs.org/docs/hooks-effect.html

about 4 years ago · Juan Pablo Isaza 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!