Answer the question
In order to leave comments, you need to log in
How to render for dom elements in react?
Good day! I'm doing a pet project, weather. I wanted to make sure that when the weather changes, the background matches the weather. Let's say it's raining, drops are dripping on the background, etc.
In the code, I render the drop positions, then assign them to the element.
let array = []
const Home = (props) => {
const weatherParts = 500
const { activeWeatherForecast } = useSelector(state => state.currentWeekday)
React.useEffect(() => {
for (let i = 0; i < weatherParts; i++) {
array.push([renderPositionY(), renderPositionX()])
}
}, [activeWeatherForecast])
const renderPositionX = () => {
return Math.floor(Math.random() * document.body.clientWidth)
}
const renderPositionY = () => {
return Math.floor(Math.random() * document.body.clientHeight)
}
...
{array.length > 0 && array.map(elem => {
return <div className={s.weatherParts} style={{ top: `${elem[0]}px`, left: `${elem[1]}px` }}></div>
})}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question