12강 실습 (섭씨 화씨)
705
작성한 질문수 1
안녕하세요 12강 실습코드를 그대로 작성했는데 섭씨온도를 적었을때 화씨온도로 자동으로 변환되어 나타나지 않습니다. 화씨온도를 먼저 적었을때도 마찬가지고요. 코드 한번 봐주실 수 있나요? 감사합니다.

Calculator
import React, {useState} from "react";
import TemperatureInput from "./Temperatureinput";
function BoilingVerdict(props){
if(props.celsius >= 100){
return <p>물이 끓습니다.</p>;
}
return <p>물이 끓지 않습니다.</p>;
}
function toCelsius(fahrenheit){
return ((fahrenheit-32)*5)/9;
}
function toFahrenheit(celsius){
return (celsius*9)/5+32;
}
function tryConvert(temperature,convert){
const input = parseFloat(temperature);
if(Number.isNaN(input)){
return "";
}
const output = convert(input);
const rounded = Math.round(output*1000)/1000;
return rounded.toString();
}
function Calculator(props){
const [temperature, setTemperature] = useState("");
const[scale,setScale]=useState("c");
const handleCelsiusChange = (temperature) => {
setTemperature(temperature);
setScale("c");
};
const handleFahrenheitChange = (temperature) => {
setTemperature(temperature);
setScale("f");
};
const celsius =
scale === "f" ? tryConvert(temperature, toCelsius):temperature;
const fahrenheit =
scale === "c" ? tryConvert(temperature, toFahrenheit):temperature;
return (
<div>
<TemperatureInput scale="c"
temperatrue = {celsius}
onTemperatureChange={handleCelsiusChange}/>
<TemperatureInput scale="f"
temperatrue = {fahrenheit}
onTemperatureChange={handleFahrenheitChange}/>
<BoilingVerdict celsius={parseFloat(celsius)} />
</div>
);
}
export default Calculator;Temperatureinput
const scaleNames = {
c:'섭씨',
f:'화씨',
};
function TemperatureInput(props){
const handleChange = (event) => {
props.onTemperatureChange(event.target.value);
};
return (
<fieldset>
<legend>
온도를 입력해주세요(단위:{scaleNames[props.scale]});
</legend>
<input value={props.temperature} onChange={handleChange} />
</fieldset>
);
}
export default TemperatureInput;
답변 1
0
안녕하세요, 소플입니다.
Calculator 컴포넌트에서 TemperatureInput 컴포넌트를 사용하는 부분에서 props를 넣어줄 때 공백이 들어가면 안 됩니다.
아래와 같이 수정해서 한 번 해보시고 그래도 안되면 다시 댓글 남겨주시기 바랍니다!
<div>
<TemperatureInput
scale="c"
temperatrue={celsius}
onTemperatureChange={handleCelsiusChange} />
<TemperatureInput
scale="f"
temperatrue={fahrenheit}
onTemperatureChange={handleFahrenheitChange} />
<BoilingVerdict celsius={parseFloat(celsius)} />
</div>
감사합니다.
강의가 삭제되었다고 합니다
0
111
1
이거 왜 존재하지 않는다고 뜨는건가요
0
138
1
존재하지 않는 수업이라고 떠요
0
184
1
안드로이드 에뮬레이터 오류
0
101
1
교재 구입해서 강의 들으려고 하는데 커리큘럼이 없어졌어요.
0
130
1
prevIsConfiromed 질문
1
144
2
chapter14 잘이해가 되지않습니다..
1
136
2
2025년 3월 리액트버전
1
205
2
npm 설치 오류
1
179
1
chapter_07 콘솔로그 질문드려요~!
1
129
2
안녕하세요 미니블로그 실습 질문드립니다.
1
179
3
에러가 떠요
1
220
3
Chapter6 질문 드립니다
1
210
2
실습 코드 있을까요?
1
208
2
상태가 업데이트될때 컴포넌트 최상단의 console.log 코드가 두번 실행되는 이유가 궁금합니다.
1
234
2
npx create-react-app my-app 명령어 입력이 잘못된 것 같습니다
0
310
3
이름과 코멘트 줄바꿈이 안 됩니다.
0
142
1
버튼이 안 뜹니다
0
305
2
npx create-react-app my-app
1
472
2
jsx 코드 작성해보기에서 index.js 수정 후 에러 뜹니다.
1
377
3
Chapter_05 터미널, 리액트 에러
0
194
2
npx create-react-app my-app 명령어 반응없음
1
434
3
import 코드 에러
1
215
1
백틱
1
122
1





