0

I am trying to render a dynamic list but inside the jsx rendered, I can't display any item this is my code, I've also tried with useState const [orderList, setOrderList] = useState([]) and setOrderList(prev => [...prev, childData]) but it returns me an empty array so I've opted for the classic javascript way but it won't work, it also won't console log the data inside the render

const OrdiniModuloVideoAds = () => {
    let orderList = [];

    const ordiniRef = ref(realtimeDatabase, "ordinazioneVideoAds/modulo/ordini");

    useEffect(() => {
        onValue(ordiniRef, (snapshot) => {
            snapshot.forEach((childSnapshot) => {
                const childData = childSnapshot.val();
                orderList.push(childData);
            });
            console.log(orderList);
        });
    }, []);

    return (
        <StyledOrdiniModuloVideoAds>
            <div className='ordiniWrapper'>
                {orderList.map((i) => {
                    return (
                        <span>{i.mail}</span>
                    );
                })}
            </div>
        </StyledOrdiniModuloVideoAds>
    );
};

EDIT This is the snippet with the useState:

const OrdiniModuloVideoAds = () => {
    const [orderList, setOrderList] = useState([])

    const ordiniRef = ref(realtimeDatabase, "ordinazioneVideoAds/modulo/ordini");

    useEffect(() => {
        onValue(ordiniRef, (snapshot) => {
            snapshot.forEach((childSnapshot) => {
                const childData = childSnapshot.val();
                setOrderList((prev) => [...prev, childData])
            });
            console.log(orderList);
        });
    }, []);

    return (
        <StyledOrdiniModuloVideoAds>
            <div className='ordiniWrapper'>
                {orderList.map((i) => {
                    return (
                        <span>{i.mail}</span>
                    );
                })}
            </div>
        </StyledOrdiniModuloVideoAds>
    );
};

The data is actually added because it logs to me the array on the useEffect Any suggestion?

2
  • can you show us the implementation that you have done with useState? Commented Sep 3, 2022 at 9:03
  • 1
    Bro, you can't render your list with a standard javascript! Let me create a solution try that code and let me know if that code works for you or not. Commented Sep 3, 2022 at 10:07

2 Answers 2

1

This is because your map callback does not return anything:

<div className='ordiniWrapper'>
  {array.map((i) => {
    return (
     <span>{i.mail}</span>
    );
  })}
</div>

Or the short version:

<div className='ordiniWrapper'>
  {array.map((i) => (
    <span>{i.mail}</span>
  ))}
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

You are right. But I don't think updating the array directly by its ref is a good practice. He should use useState instead.
Sorry I've forgot to add it, it also doesn't work with the return
0

Try this solution hopefully it will fix your issue.


const OrdiniModuloVideoAds = () => {
    const [orderList, setOrderList] = React.useState([]);

    const ordiniRef = ref(realtimeDatabase, "ordinazioneVideoAds/modulo/ordini");

    useEffect(() => {
        onValue(ordiniRef, (snapshot) => {
            snapshot.forEach((childSnapshot) => {
                const childData = childSnapshot.val();
                setOrderList(prev => ([...prev, childData])); /// Order list array is empty because you're not returning the data properly that's why it just gives you the default empty array in the console.
            });
        });
    }, []);

    return (
        <StyledOrdiniModuloVideoAds>
            <div className='ordiniWrapper'>
                {orderList.map((i) => <span key={i.mail}>{i.mail}</span>)}
            </div>
        </StyledOrdiniModuloVideoAds>
    );
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.