import React, { useEffect } from 'react';
import { useCookies } from 'react-cookie';

export default function DjangoCSRFToken({ csrfToken }) {
  const [cookies, setCookie] = useCookies(['csrftoken']);

  useEffect(() => {
    if (csrfToken && csrfToken !== cookies.csrftoken) {
      setCookie('csrftoken', csrfToken, { path: '/' });
    }
  }, [csrfToken, cookies.csrftoken, setCookie]);

  return (
    <input type="hidden" value={cookies.csrftoken} name="csrfmiddlewaretoken" />
  );
}
