Home > database >  Why my call map function is hide in the count nodeList but it is visible in return output?
Why my call map function is hide in the count nodeList but it is visible in return output?

Time:05-18

This will be a complex sample cause my codes is like a lot...

So basically I just direct to my point. My problem is that I don't know why my list map function is not includes the output function of return when I'm trying to get the document.querySelectorAll() of all the items. Basically something like this.

            <div className='gallery' style={ ispost ? { display:'none' } : {display:"block"}}>
                {mainList.map((elem,idx) => {
                    return (
                        <div className="gallery-container" key={idx}>
                            <div className="l">
                                <div className="l-box">
                                    <div className="text">
                                        <div className="hide">
                                            <h2> Onigsahima Tokyo </h2>
                                        </div>
                                    </div>
                                    <div className="hide">
                                        <div className="color-sample">
                                            <img src={g3} alt="" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div className="r">
                                <div className="r-box">
                                    <div className="hide">
                                        <div className="color-sample">
                                            <img src={g3} alt="" />
                                        </div>
                                    </div>
                                </div>
                                <div className="r-box">
                                    <div className="hide">
                                        <div className="color-sample">
                                            <img src={g3} alt="" />
                                        </div>
                                    </div>
                                </div>
                                <div className="r-box">
                                    <div className="hide">
                                        <div className="color-sample">
                                            <img src={g3} alt="" />
                                        </div>
                                    </div>
                                </div>
                                <div className="r-box">
                                    <div className="hide">
                                        <div className="color-sample">
                                            <img src={g3} alt="" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    )
                })}
                <div className="gallery-container" style={ ispost ? { display:'none' } : {display:"inline-grid"}}>
                    <div className="l">
                        <div className="l-box">
                            <div className="text">
                                <div className="hide">
                                    <h2> Onigsahima Tokyo </h2>
                                </div>
                            </div>
                            <div className="hide">
                                <div className="color-sample">
                                    <img src={g3} alt="" />
                                </div>
                            </div>
                        </div>
                    </div>
                    <div className="r">
                        <div className="r-box">
                            <div className="hide">
                                <div className="color-sample">
                                    <img src={g3} alt="" />
                                </div>
                            </div>
                        </div>
                        <div className="r-box">
                            <div className="hide">
                                <div className="color-sample">
                                    <img src={g3} alt="" />
                                </div>
                            </div>
                        </div>
                        <div className="r-box">
                            <div className="hide">
                                <div className="color-sample">
                                    <img src={g3} alt="" />
                                </div>
                            </div>
                        </div>
                        <div className="r-box">
                            <div className="hide">
                                <div className="color-sample">
                                    <img src={g3} alt="" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
             </div>

And then I have a useEffect() and I'll show all the codes about it..but don't get confused about it just focus on the querySelectorAll() cause it is the only thing that affect it all the animation

    useEffect(() => {

        gsap.registerPlugin(ScrollTrigger,CSSRulePlugin)
        // gsap.registerPlugin(CSSRulePlugin)
        const sections_ = document.querySelectorAll('.gallery-container')
        console.log(sections_)
        let sections = gsap.utils.toArray(".gallery-container")
        console.log(sections)
        let scrollTween = gsap.to(sections, {
            xPercent: -100 * (sections.length - 1),
            ease: "none", // <-- IMPORTANT!
            scrollTrigger: {
                trigger: ".gallery",
                pin: true,
                scrub: 3,
                end: " =3000",
                // markers:true
            }
        });

        
        gsap.to(".gallery-container:nth-child(1) .color-sample",{
            y:0,
            ease:"none",
            scrollTrigger:{
                trigger:".gallery-container:nth-child(1)",
                containerAnimation:scrollTween,
                start:"0% 0%",
                end:"10% 6%",
                // markers:{startColor: "orange", endColor: "green"},
                scrub:2,
            }
        })
        const rule = CSSRulePlugin.getRule(`.r-box:after`)
        const rule2 = CSSRulePlugin.getRule(`.r-box:before`)
        gsap.to([rule,rule2],{
            width:'35%',
            height:'35%',
            background:"yellow",
            delay:3,
            ease:"none",
            scrollTrigger:{
                trigger:`.gallery-container:nth-child(1)`,
                containerAnimation:scrollTween,
                start:"0% 0%",
                end:"10% 6%",
                // markers:true,
                scrub:2,
            }
        })

        gsap.to(`.gallery-container:nth-child(1) .text h2`,{
            y:0,
            // delay:2,
            ease:"none",
            scrollTrigger:{
                trigger:`.gallery-container:nth-child(1)`,
                containerAnimation:scrollTween,
                start:"0% 0%",
                end:"10% 6%",
                // markers:true,
                scrub:2,
            }
        })

        sections.forEach((elem,i) => {
            gsap.to(`.gallery-container:nth-child(${i 2}) .color-sample`,{
                y:0,
                ease:"none",
                scrollTrigger:{
                    trigger:`.gallery-container:nth-child(${i 2})`,
                    containerAnimation:scrollTween,
                    start:"center 50%",
                    end:"center 60%",
                    // markers:true,
                    scrub:2,
                }
            })

            gsap.to(`.gallery-container:nth-child(${i 2}) .text h2`,{
                y:0,
                // delay:2,
                ease:"none",
                scrollTrigger:{
                    trigger:`.gallery-container:nth-child(${i 2})`,
                    containerAnimation:scrollTween,
                    start:"center 50%",
                    end:"center 60%",
                    // markers:true,
                    scrub:2,
                }
            })

            const rule = CSSRulePlugin.getRule(`.r-box:after`)
            const rule2 = CSSRulePlugin.getRule(`.r-box:before`)
            gsap.to([rule,rule2],{
                width:'35%',
                height:'35%',
                background:"yellow",
                delay:3,
                ease:"none",
                scrollTrigger:{
                    trigger:`.gallery-container:nth-child(${i 2})`,
                    containerAnimation:scrollTween,
                    start:"center 50%",
                    end:"center 60%",
                    // markers:true,
                    scrub:2,
                }
            })

        })


        const arrowsColor = document.querySelectorAll('.arrow.first ion-icon')
        const arrows2Color = document.querySelectorAll('.arrow.second ion-icon')

        var scrollPos = 0 
        window.addEventListener('scroll',() => { 
            if ((document.body.getBoundingClientRect()).top > scrollPos) {
                // console.log('Right')
                arrowsColor.forEach(elem => {
                    elem.style.color = 'black'
                })
                arrows2Color.forEach(elem => {
                    elem.style.color = 'yellow'
                })
            } else {
                // console.log('Left')
                arrowsColor.forEach(elem => {
                    elem.style.color = 'yellow'
                })
                arrows2Color.forEach(elem => {
                    elem.style.color = 'black'
                })
            }
            scrollPos = (document.body.getBoundingClientRect()).top
        })

    },[])

I don't know if this is easiest way to explain it but this is the best I can.. Note that mainList is a useState so I'll have something like this in my codes when I am pushing the items in setMainList

    useEffect(() => {
        axios.get('http://localhost:7777/gallery')
        .then(res => {
                let del = []
                res.data.map((elem,i) => {
                    console.log(i)
                    setMainList([...mainList,elem]) 
                })
            })
    },[])

Yes it works but why the document.querySelectorAll don't detect if there is a new element that I pop in that mainList.map function? I don't understand why..It shouldn't be this kind of way. I always do this but never encounter this bug.. Note that mainList.map() is like visible but it is not include in that querySelectorlAll Please help badly need it to get this thing done.

CodePudding user response:

Because your useEffect callback runs only once since you haven't added any dependencies. If you want the callback to be invoked whenever mainList changes, add mainList to the useEffect dependency list like so

useEffect(() => {
  //...
}, [mainList])

Be careful though! You should use the return callback to remove any listeners or subscriptions you've set up.

useEffect(() => {
  //...
  
  function onScroll() {
    //...
  }
  window.addEventListener('scroll', onScroll)

  return () => {
    // remove listeners
    window.removeEventListener('scroll', onScroll)
  }
}, [mainList])

I don't know how gsap works, but you might also want to "deregister" the plugin in the tear-down callback, or just use a different useEffect callback with no dependencies to register it only once - you probably still want to "deregister" it either way, but you wont do it every time mainList changes.

Edit: I highly suggest you read the useEffect docs

Edit2: Here's a gist

  • Related