score:1

Accepted answer

the state value q is undefined the first time the page loads because you're not setting an initial value when you created the state like this

const [q, setquiz] = usestate();

to resolve this issue you have to either pass in the initial value during the state definition like this

const initialquizvalue = {} // whatever defaults you want

const [q, setquiz] = usestate(initialquizvalue);

or check that q is available before rendering the formik component like this

const quizedit = () => {
  const { id } = useparams();
  const [focused, setfocused] = usestate(false);
  const [submitbtnhover, setsubmitbtnhover] = usestate(false);
  const [q, setquiz] = usestate()

  useeffect(() => {
    ...
  }, []);

  if (!q) {
    return 'loading quiz data...'
  }

  return (
    <div classname='formiknewquiz'>
      <formik
        initialvalues={{
          question: q["question"],
          answers: ['', ''],
          correctanswer: '',
          category: '',
          createdat: new date(),
          likes: 0,
        }}
      >
        ...
      </formik>
  )
}

Related Query

More Query from same tag