How to change React-Hook-Form defaultValue with useEffect()?

I am creating a page for user to update personal data with React-Hook-Form.
Once paged is loaded, I use useEffect to fetch the user’s current personal data and set them into default value of the form.

I put the fetched value into defaultValue of <Controller />.
However, it is just not showing in the text box.
Here is my code:

import React, {useState, useEffect, useCallback} from 'react';
import { useForm, Controller } from 'react-hook-form'
import { URL } from '../constants';

const UpdateUserData = props => {
    const [userData, setUserData] = useState(null);
    const { handleSubmit, control} = useForm({mode: 'onBlur'});

    const fetchUserData = useCallback(async account => {
        const userData = await fetch(`${URL}/user/${account}`)
                            .then(res=> res.json());
        console.log(userData);
        setUserData(userData);
    }, []);

    useEffect(() => {
        const account = localStorage.getItem('account');
        fetchUserData(account);
    }, [fetchUserData])

    const onSubmit = async (data) => {
        // TODO
    }

    return (
        <div>
            <form onSubmit={handleSubmit(onSubmit)}>
                <div>
                    <label>User Name:</label>
                    <Controller
                        as={<input type='text' />}
                        control={control}
                        defaultValue={userData ? userData.name : ''}
                        name='name'
                    />
                </div>
                
                <div>
                    <label>Phone:</label>
                    <Controller
                        as={<input type='text' />}
                        control={control}
                        defaultValue={userData ? userData.phone : ''}
                        name='phone'
                    />
                </div>
                <button>Submit</button>
            </form>
        </div>
    );
}

export default UpdateUserData;

The called API is working well and the value is actually set to userData state.

{
  name: "John",
  phone: "02-98541566"
  ...
}

I also tried to setUserData with mock data in useEffect(), and it doesn’t work either.
Is there any problem in my above code?

146 thoughts on “How to change React-Hook-Form defaultValue with useEffect()?”

  1. Pingback: viagra buy online
  2. therapie act exercices therapie respiratoire , therapie sexofonctionnelle formation therapie act quebec , Premarin prix sans ordonnance, Acheter Premarin comprimГ© pas cher. Premarin sans ordonnance Suisse Premarin pas cher [url=https://reserve12sor.asso-web.com/#]Premarin livraison Suisse[/url] pharmacie carrefour nice lingostiere Solupred prix sans ordonnance, Acheter Solupred 10 mg pas cher. Solupred sans ordonnance Belgique Solupred pas cher [url=https://docrwem.asso-web.com/#]Solupred Prednisolone 10 mg[/url] , pharmacie buisson annecy pharmacie amiens rue jean catelas , traitement mycose pied. pharmacie lafayette nimes Acheter Donecept alzheimers pas cher, Donecept livraison France. Donecept sans ordonnance France Donecept pas cher [url=https://francevspred.asso-web.com/#]Donecept Donepezil alzheimers[/url] , pharmacie auchan nice pharmacie zanetti, pharmacie de garde aujourd’hui porto vecchio pharmacie rue valmy brest . pharmacie de garde yssingeaux aujourd’hui, pharmacie franconville pharmacie de garde joigny pharmacie cours d’orbitelle aix en provence .

    Reply
  3. Pingback: sildenafil 100mg
  4. Pingback: sildenafil viagra
  5. Pingback: cheap cialis
  6. Pingback: cialis for sale
  7. Pingback: sildenafil 20 mg
  8. Pingback: discount viagra
  9. Pingback: buy cialis
  10. Pingback: generic for viagra
  11. Pingback: cialis capsule
  12. Pingback: cheap viagra
  13. Pingback: sex pills cialis
  14. Pingback: cialis rezeptfrei
  15. Pingback: discount cialis
  16. Pingback: buy sildenafil
  17. Pingback: free viagra
  18. Pingback: viagra cvs
  19. Pingback: