next.js 如何使用[proifle.js]文件Reactjs更新表单

toe95027  于 2022-12-18  发布在  React
关注(0)|答案(1)|浏览(124)

我正在使用Reactjs和Nextjs框架,现在我正在管理面板上工作,并尝试更新用户配置文件,为此,我创建了文件“[profile.js]”并传递userId,我尝试在表单内获取用户信息,并希望更新表单意味着每当用户点击“更新”时,信息应通过Axios更新(API已经创建),但是我如何使用“form submit”?换句话说,我如何在nextjs中获得form/use函数的值?以下是我当前的代码

const Post = ({ post }) => {
    // want to get form data and .....further code
}

return (
    <>
         <form className="forms-sample" >
             <input
                 type="text"
                 className="form-control"
                 id="exampleInputName1"
                 placeholder="Title"
                 name="name"
                 value={Post.title}/> //not showing
             <button type="submit" className='btn btn-primary mr-2'>Submit</button>     
         </form>
    </>
);
    
export const getStaticProps = async ({ params }) => {
    const { data } = await Axios.get(`xxxxxxxxxxxxxxxxxxxxxx/getblogbyuserid/${params.slug}`);
    const post = data;
    return {
        props: {
            post,
        },
    };
};

export const getStaticPaths = async () => {
    const { data } = await Axios.get("xxxxxxxxxxxxxxxxxxxxxxxxxxxx/api/blogs");
    const posts = data.slice(0, 10);
    const paths = posts.map((post) => ({ params: { slug: post.id.toString() } }));
    return {
        paths,
        fallback: true,
    };
};
kyvafyod

kyvafyod1#

你可以这样做:

const submitHandler = (formData) => {
   // your code
}

<form className="forms-sample" onSubmit={submitHandler}>

相关问题