简体   繁体   English

当我在 useEffect 中设置状态时,组件没有呈现

[英]Component didn't render when I setState in useEffect

Why my component didn't do the render again when I setState inside my useEffect?为什么当我在我的 useEffect 中设置状态时我的组件没有再次渲染? I read on google that the component is render another time if I setState inside the useEffect我在谷歌上读到,如果我在 useEffect 中设置状态,组件将再次渲染

My code:我的代码:

export default function CarouselMusicGenres() {
    const [musicGenres, setMusicGenres] = useState(null)

    const setAllMusicGenres = () => {
        MusicGenresAPI.getAll()
            .then((response) => {
                if (response.status === 200) {
                    setMusicGenres(response.data.musicGenres)
                }
            })
            .catch((error) => {
                console.log(error)
            })
    }

    const displayAllMusicGenres = () => {
        if (musicGenres && musicGenres.length > 0) {
            musicGenres.forEach((musicGenre) => {
                return (
                    <SwiperSlide>
                        <Col
                            className="genre"
                            style={{
                                backgroundImage: `url(/assets/images/music-genres/${musicGenre.image_background_url})`,
                            }}
                        >
                            <span>{musicGenre.genre}</span>
                            <div className="transparent-background"></div>
                        </Col>
                    </SwiperSlide>
                )
            })
        }
    }

    useEffect(() => {
        setAllMusicGenres()
    }, [])

    return (
        <Row className="carousel-genres">
            <Swiper spaceBetween={10} slidesPerView={6}>
                {displayAllMusicGenres()}
            </Swiper>
        </Row>
    )
}

I try someting like that instead useEffect but still don't work我尝试了类似的东西,而不是 useEffect 但仍然不起作用

if (!musicGenres) {
        setAllMusicGenres()
}

Problem Fixed问题已修复

I need to use map instead foreach to get the array returned and add return before my musicGenres.map我需要使用 map 而不是 foreach 来获取返回的数组并在我的 musicGenres.map 之前添加 return

in order to return an array you should use map and you need to add something in your dependency array if you want to rerender on change.为了返回一个数组,你应该使用 map 如果你想在更改时重新渲染,你需要在依赖数组中添加一些东西。 when you do it like that当你这样做时

useEffect(() => {
    setAllMusicGenres()
}, [])

you are only telling it to setAllMusicGenre on load and not on change, if you would like it to change when a certain change happens to musicGenres you should add it to the array.您只是告诉它 setAllMusicGenre 在加载时而不是在更改时,如果您希望它在 musicGenres 发生特定更改时更改,则应将其添加到数组中。

useEffect(() => {
    setAllMusicGenres()
}, [musicGenres ])

you need to add dependency in order to Render it, again and again, for example, In your case musicGenres state will be your dependency so you need to write your useState Like below -您需要添加依赖项才能一次又一次地渲染它,例如,在您的情况下, musicGenres state 将是您的依赖项,因此您需要编写 useState 如下所示 -

    useEffect(() => {
    setAllMusicGenres()
},[musicGenres])

It will update your component whenever you update musicGenres Note: You need to Pass Key(It should always be unique) also So DOM can Understand Something change and it will Update accordingly每当您更新 musicGenres 时,它都会更新您的组件注意:您还需要传递密钥(它应该始终是唯一的)所以 DOM 可以理解某些变化并且它会相应地更新

When you write useEffect like given below in hooks -当您在钩子中编写下面给出的 useEffect 时 -

useEffect(() => {
        setAllMusicGenres()
    }, [])

it will act Like componentDidMount它会像 componentDidMount

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM