Striving to convert Celsius to Fahrenheit using ReactJS

I am currently attempting to convert Celsius into Fahrenheit within this specific div. Despite being fairly new to React, I’m finding it challenging to determine where exactly to place the conversion calculation.

return (
    <div className="app">
        <main>
            <div className="search-box">
                <input
                    type="text"
                    className="search-bar"
                    placeholder="Search"
                    onChange={e => setQuery(e.target.value)}
                    value={query}
                    onKeyPress={search}
                />
            </div>
            {(typeof weather.main != "undefined") ? (<div>
                <div className='location-box'>
                    <div className='location'>{weather.name}, {weather.sys.country}</div> <div className='date'>{dateBuilder(new Date())}</div>
                </div>
                <div className="weather-box">
                    <div className="celsius-temp">{Math.round(weather.main.temp)}°C </div>
                    <div className="fahrenheit-temp">{Math.round(weather.main.temp)}°F </div>
                    <div className="weather">{weather.weather[0].main} </div>
                </div>
            </div>
            ) : ('')}
        </main>
    </div>
)

The goal is to display two values for temperatures: one in Celsius (retrieved from the weather API) and another in Fahrenheit (converted from the Celsius value).

I attempted to perform ({Math.round(weather.main.temp)} * 9/5) + 32 within the element, but encountered issues as shown in the image provided. Should I consider defining a constant value within the function at the top or inside the function App()?

Your assistance would be greatly appreciated.

Answer №1

Remember, the curly braces {} contain code that will be executed while anything outside of them will simply display as is.

For example, don't do this:

<span className="celsius-temp">({Math.round(weather.main.temp)} * 9/5) + 32</span>

Instead, go for:

<span className="celsius-temp">{(Math.round(weather.main.temp) * 9/5) + 32}</span>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to prevent the second ul from appearing?

Seeking assistance with navigation. When clicking on the "Athletics" tab in the provided link, the second ul displays. Is there a method to hide this second ul and have it only appear upon hovering? ...

Every time I attempt to start my React app using npm start, I encounter the error message stating "Something is already running on port 3000"

As I ventured into creating a fake REST API using Express JS and deploying it to Heroku today, I found myself following a YouTube tutorial due to my lack of experience. In the app.js file, I added the following code: app.listen(process.env.PORT || 3000, () ...

I'm unable to import correctly using the --compiler option

Having an issue here. I'm trying to bring in the typescript compiler. I used this command: bit import bit.envs/compilers/typescript --compiler Unfortunately, it didn't work. This is the error message: bit import [ids...] import components in ...

Numerous objects come into view as you scroll

I found a code snippet online that animates items as they appear on scroll. However, the code only triggers animation for one item at a time. How can I modify the code to trigger the animation for all items? const observer = new IntersectionObserver(e ...

What is the best strategy for managing a sizable react application using react-query?

Since diving into React with functional components and react-query, I've been facing some confusion on how to properly organize my components. Typically, I design components by having a top-level component handle all data access and pass data down to ...

Is there a way to modify a component's CSS by using a global CSS class name in Angular?

We have implemented a system where a class on the html element determines whether the user is in dark or light mode of the application. <html class="dark-mode"></html> This class is dynamically added using Renderer2 in a service that detects ...

The module was not found: Unable to locate the file './AppAPI' in the project directory

Despite setting up my react app correctly, I keep encountering an error. I have all my code in a file called App.API.js within the src folder. The issue seems to be originating from the index.js file, where the code is: import App from './AppAPI&apo ...

Focusing on a specific div element

I need help selecting the initial div block that contains the word "Target". <div class="box-content"> <div> <div>Target</div> <div>xxxx</div> <div>xxxx</div> <div>xxxx</div> ...

Navigate from Product Listing to Product Details Page by utilizing JSON data with ReactJS

I have successfully retrieved the initial level of JSON Data, which consists of objects/Product List. My objective is to implement a functionality where clicking on the "View Details" button will redirect to the respective product detail page, similar to ...

The bullets in the HTML unordered list are partially hidden from view

My unordered list is experiencing issues with displaying bullets correctly. In Firefox and Internet Explorer, the bullets are only partially shown, while in Chrome they are not visible at all. Adding margin-left: 5px to the <li> element resolves this ...

Preserve the checkbox state upon refreshing the page

I am facing an issue with keeping the checkbox state saved after a page reload. Currently, I have stored my unchecked checkboxes in localStorage, but I am unsure about what steps to take next. In simple terms, I want the checkbox to remain unchecked when I ...

Tips for circumventing the use of responsive tables in Buefy

I am facing a challenge with displaying data in a Buefy table in a way that it appears as a conventional table with columns arranged horizontally on all devices, rather than the stacked cards layout on mobile. In order to accommodate the content appropriat ...

What could be causing the horizontal scroll bar to appear on the Web Page, even though it should fit perfectly on

I've designed this website to perfectly fit within the browser window, but for some reason, there's a horizontal scrollbar appearing. When a site fits properly, there shouldn't be a need for a horizontal scroll bar. I double-checked my zoom ...

What steps should we take to debug and log API calls in React Server Components, such as in a Next.js application directory?

Introduction Most of us are familiar with debugging API calls in Next.js using pagesDir, which allows for easy troubleshooting in the browser or through node inspector in the development terminal. However, this functionality is not readily available in Ne ...

Maximizing React's potential: Triggering parent events instead of child events for optimal event propagation

In my quest to find a solution in a React component where I want to prevent an event for the CHILD element and trigger an event for the PARENT element, I encountered an interesting scenario. Imagine a component with an array of item objects in its state, ...

Modify a property of an object using the useState Hook, where the property name is dynamically retrieved from a variable

const [fee, setFee] = useState({newPatient:'',establishedPatient:''}) const field1='newPatient' const field2='establishedPatient' To modify the fee object properties, I am looking to dynamically assign ...

What could be the reason my SVG images are not displaying?

My website contains SVGs that are not loading consistently. I acquired some from freeicons/icon8 and created a couple using Inkscape. Initially, I inserted them using HTML but switched to CSS after doing research online on how to ensure they load properly ...

Could the long-term consequences of utilizing '--force' or '--legacy-peer-deps' be detrimental?

I'm currently working on a react-native project and encountering an error while trying to install the native-base library... npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-prote ...

I am unable to determine if I have already selected a List Item

My goal is to have a functionality where clicking on "Download Drivers" will open the list, and clicking again will close it. This should be achieved with onclick events only, no hover effects. Additionally, I want the list to remain open even if I click o ...

What is the best way to transmit data from a react form to a backend API and receive a response in return?

I'm in the process of developing a login and sign-up system for my website, but I've hit a roadblock. I've implemented a state variable that toggles the page layout between displaying the login form when true and the sign-up form when false ...