在我的应用程序中,我将一些天气数据作为道具传递给组件,并且我希望根据该值获得临时更新的颜色。这就是我所做的。似乎当道具改变时反应并不总是重新渲染。我如何确保每次道具更改时都会更新?
const Current = (props) => {
const { weather } = props
const [color, setColor] = useState([])
useEffect(() => {
setColor(tempScheme[weather.current.temp_f])
}, [weather, color])
return (
<Container>
<CardContent>
<Header>
<Typography variant='h6'>Currently</Typography>
<Typography variant='h6'> {formatDate(props.weather.location.localtime)} </Typography>
</Header>
<Weather>
<Typography variant='h5'> {props.weather.current.condition.text} </Typography>
<Typography variant='h2' style={{"color": `rgb(${color})`}}> {Math.round(props.weather.current.temp_f)}° </Typography>
</Weather>
<Location>
<Typography variant='body1'> {props.weather.location.name}, {props.weather.location.region}</Typography>
<Image src={props.weather.current.condition.icon} />
</Location>
</CardContent>
</Container>
)
}这里