So what I want to do is take the input from the radio buttons (metric or imperial) from Forecast.js, use them in a function in Conditions.js to determine whether it's hot or cold. I've exported the variable unit from Forecast.js but when I try to use it in the clothes() function it doesn't work. I've tried everything I found online and I tried to understand but I'm new to Reactjs.
Conditions.js https://pastebin.com/RHbLuqZD
import { unit } from '../Forecast/Forecast.js';
function clothes(t) {
if (unit === "metric") {
if (Number('t') <= 20)
return (<>
<img src={Cold} alt="wind icon" style={{ width: 50, marginRight: 20, height: 50 }} />
It's cold, dress accordingly!
</>);
else
return (<>
<img src={Hot} alt="wind icon" style={{ width: 50, marginRight: 20, height: 50 }} />
It's Warm, dress accordingly!
</>);
}
if (unit === "imperial") {
if (Number('t') <= 70)
return (<>
<img src={Cold} alt="wind icon" style={{ width: 50, marginRight: 20, height: 50 }} />
It's cold, dress accordingly!
</>);
else
return (<>
<img src={Hot} alt="wind icon" style={{ width: 50, marginRight: 20, height: 50 }} />
It's Warm, dress accordingly!
</>);
}
}
Forecast.js https://pastebin.com/wcBu8bwA
export default Forecast;
export let unit = this.state.unit;
The reason it doesn't work could be because I didn't do the import and export properly or because I didn't use the imported variable properly.
Edit: As I specified I'm a beginner, I don't know a lot, what I want is to see how to solve the problem and then understand how to do it. I just need to understand the problem with my code, or an alternative.
thisexactly is? because it's a global variable inside of a javascript file it is thewindowobjectthiswhen used outside of amethodrefer to thewindowobject and thatwindowobject doesn't have anystateproperty in it.ForecastfunctionNumber('t')is not going to work, it will convert a string"t"to number.Number(t)should be the correct. I would, if I were you, try some better name for this variable, though. Also, you don't need theelsethere, as you are returning after theif.