Getting the data from the contract at initialization

Hi guys,

I have a state const [arrayAddress, setArrayAddress] = useState([]); and I set the arrayAddress when I call my contract :

<Button
                text='Ajouter le voter'
                theme='primary'
                type='button'
                icon='arrowCircleDown'
                onClick={async () =>
                    await voting({
                        onSuccess: (mess) => {
                            setArrayAddress((prevArray) =>[...prevArray, address])
                            handleSuccess(mess, 'info', `L\'adresse ${address} a bien été ajouté`, 'Ajout d\'une nouvelle adresse', 'bell')
                            console.log(mess)
                            console.log('array',arrayAddress)
                        },
                        onError: (err) => {
                            handleError(err, `${err.error ? err.error.message : err}`, 'Erreur', 'xCircle')
                        }
                    })
                }
            />

Then I display it. Everything works except when I refresh the page, my arrayAddress is empty !
So it means I’ve got the data in the blockchain but at the initialization I don’t have the info in my arraAddress and I should get the it from my contract and set it ( this is what I think, maybe I’m wrong)
But I don’t know how to do it, any idea ?

It depends on what functions the contract has in order to get that information from the contract in the first place. You need a read only function in the contract that can return that information that you want to show.
After that you have to call that contract read only function when the page loads.

I can’t modify the contract, I’m in the training to become developper BC (I’m the only one who choose moralis for the project :muscle: ) and I can’t modify the contract…

There is another option get the events for that contract, sync them with a server and then read them from there. In case that the contract function emits an events with all the needed information.

You can also check what contract already has as available functions, maybe there is a function that can help you.