【问题标题】:Rendered more hooks than during the previous render. when posting form data with React Hooks比上一次渲染时渲染了更多的钩子。使用 React Hooks 发布表单数据时
【发布时间】:2019-12-22 10:23:57
【问题描述】:

今天遇到了挂钩问题。我知道有一个类似的帖子,我阅读了使用钩子的规则。现在,当我发布表单时,它给了我这个错误。我知道那是因为我的钩子在 if 语句中。但我怎样才能把它弄出来?如果它不在函数或语句中,我不知道如何使用这个钩子。任何建议将不胜感激。代码如下:

import React, { FunctionComponent, useState, useEffect } from 'react';
import usePost from '../hooks/usepost'
import Article from './article';

interface ArticlePosted {
    title: string,
    body: string,
    author: string
}
const Post: FunctionComponent = () => {

    const [details, detailsReady] = useState({})
    const postArticle = (e) => {
        e.preventDefault()
        const postDetails = {
            title: e.target.title.value,
            body: e.target.body.value,
            author: e.target.author.value
        }
        detailsReady(postDetails)
    }

    if (Object.keys(details).length !== 0) {
        console.log(details)
        usePost('http://localhost:4000/kb/add', details)
    }
    return (
        <div>
            <form onSubmit={postArticle}>
                <p>
                    Title <input type='text' name='title' />
                </p>
                <p>
                    Body <textarea name='body' rows={4} />
                </p>
                <p>
                    Author <input type='text' name='author' />
                </p>
                <button type='submit'>Submit Article</button>

            </form>
        </div>
    );
};

export default Post;

自定义挂钩:

import React, { useState, useEffect } from 'react';
import axios from 'axios';

const usePost = (url, postDetails) => {
    //set empty object as data
    console.log(url, "DFLSKDJFSDLKFJDLKJFDLFJ")

    console.log(postDetails)
    useEffect(() => {
        console.log('usePost running')
        axios.post(url, postDetails)
            .then(res => {
                console.log(res)
                return
            })
    }
        , [postDetails]);

};

export default usePost

【问题讨论】:

    标签: reactjs react-hooks


    【解决方案1】:

    您可以将 if 语句逻辑移动到 usePost 挂钩中。

    const usePost = (url, postDetails) => {
        useEffect(() => {
            if (Object.keys(postDetails).length === 0){
                return console.log('Not posting'); // Don't post anything if no details
            }
            // Otherwise, post away
            console.log('usePost running')
            axios.post(url, postDetails)
                .then(res => {
                    console.log(res)
                    return
                })
        }
            , [postDetails]);
    
    };
    

    【讨论】:

    • 那么它怎么知道hen从post组件发送数据
    • 我将 if 语句移到了 useEffect 中。如果postDetails 包含任何内容,效果挂钩将发布到 url,否则它将提前返回并停止运行其余功能。
    • 很简单,你的解决方案很棒。谢谢!
    • 仅仅因为某些东西有效并不意味着它是好的代码。你为什么还要使用useEffect? @lakerskill
    • @FabricioG 我认为lakerskill 不想在每次重新渲染时都重新发布细节,如果它们保持不变的话。这可以通过手动if (postDetails === prevPostDetails) 来完成,但这更容易。
    猜你喜欢
    • 2021-05-31
    • 2020-05-26
    • 1970-01-01
    • 2020-11-26
    • 2019-12-24
    • 2021-10-12
    • 2021-02-09
    • 2021-01-06
    • 2023-02-13
    相关资源
    最近更新 更多