function BoilingVerdict(props) {
if (props.celsius >= 100) {
return (
<p>The water would boil.</p>;
)
}
return (
<p>The water would not boil.</p>
)
}
BoilingVerdict
Componentfunction Calculator (props) {
const [temperature, setTemperature] = useState('')
return (
<fieldset>
<legend>Enter temperature in Celsius:</legend>
<input
value={temperature}
onChange={(evt) => {
setTemperature(evt.target.value)
}} />
<BoilingVerdict
celsius={parseFloat(temperature)} />
</fieldset>
);
}
function Calculator (props) {
return (
<div>
<TemperatureInput scale="c" />
<TemperatureInput scale="f" />
</div>
);
}
function TemperatureInput (props) {
const [temperature, setTemperature] = useState('')
const scale = props.scale;
return (
<fieldset>
<legend>Enter temperature in {scaleNames[scale]}:</legend>
<input value={temperature}
onChange={(evt) => {
setTemperature(evt.target.value)
}
}
/>
</fieldset>
);
}
}
Calculator
to achieve thisfunction TemperatureInput (props) {
/* Send changes to the parent */
handleChange(evt) {
props.setTemperature(evt.target.value);
props.setScale(props.scale)
}
/* Use READ-ONLY Props instead of State */
return (
<fieldset>
<legend>Enter temperature in {scaleNames[scale]}:</legend>
<input value={props.temperature}
onChange={this.handleChange} />
</fieldset>
);
}
setTemperature
from props with new statesetTemperature
function Calculator (props) {
const [scale, setScale] = useState('c')
const [temperature, setTemperature] = useState('')
const celsius = scale === 'f' ? tryConvert(temperature, toCelsius) : temperature;
const fahrenheit = scale === 'c' ? tryConvert(temperature, toFahrenheit) : temperature;
return (
<div>
<TemperatureInput
scale="c"
temperature={celsius}
setScale={setScale}
setTemperature={setTemperature}
/>
<TemperatureInput
scale="f"
temperature={fahrenheit}
setScale={setScale}
setTemperature={setTemperature}
/>
<BoilingVerdict
celsius={parseFloat(celsius)} />
</div>
);
}
}
See the Pen djrKLj by Joshua Burke (@Dangeranger) on CodePen.
Let's create a React component that consists of two main components; a list of posts, and a display box. These should be two seperate components. You will also need a parent component that renders both the list, and the display box.
Using state and props set up the page so that when you click on a post the cotent is rendered in the display box.
Hint: If you want to store your posts locally it might be easiest to create JSON files to represent the posts. Or you could just use JSONPlaceholder
/