I have a form and functions don’t work on those buttons. I am using Axios to do the post request. Is it something related to children layouts? Naming the files? It’s the fact of my children layout dont have body or main tags?
main layout
import { Inter } from "next/font/google";
import Head from 'next/head';
import "./globals.css";
const inter = Inter({ subsets: ["latin"] });
export const metadata = {
title: "Company Dashboard",
description: "",
};
export default function DashboardLayout({ children }) {
return (
<div className="mainContent">
<Head>
<title>{metadata.title}</title>
<meta name="description" content={metadata.description} />
</Head>
<header>
{/* Header content goes here */}
</header>
<body>
<div className="leftMenu">
<nav>
<a href="/"><button>Home</button></a>
<a href="/admins"><button>Admins</button></a>
<a href="/employees"><button>Employees</button></a>
<a href="/clients"><button>Clients</button></a>
<a href="/cars"><button>Cars</button></a>
</nav>
</div>
<div className="leftContent"></div>
<div className="middleContent">{children}</div>
<div className="rightContent"></div>
</body>
</div>
);
}
code in question
'use client'
import React from "react";
import axios from 'axios';
export default function employees(){
const formDataClass = {name:'',surname:'',email:''}
async function login() {
try {
const response = await axios.post("URL",formDataClass);
alert(formDataClass.name);
} catch (error) {
console.log(error);
}
}
return(
<div className="content">
<div><h1>Seja Bem Vindo ao Sistema de Gestão V1.0</h1></div>
<div className="formulary">
<div><h3>Formulário de Empregados</h3></div>
<div>
<form>
<input onChange={(e)=>formDataClass.name=e.target.value} placeholder="Insert Your Name"></input>
<input onChange={(e)=>formDataClass.surname=e.target.value} placeholder="Insert Your Surname"></input>
<input onChange={(e)=>formDataClass.email=e.target.value} placeholder="Insert your Email"></input>
<div className="formButtons">
<button type="button">Save</button>
<button type="button" onClick={login()}>Submit</button>
</div>
</form>
</div>
</div>
</div>
);
}
I did try everything and expect the buttons to trigger the axios post
2
Answers
Try one of below?
Since you’re using React you should be using state to manage your form data.
Your button should be
onClick={login}
becauseonClick={login()}
assigns the value of the result of callinglogin
to your event handler rather than a reference to the function itself.