为什么React Component渲染两次

时间:2019-10-26 16:53:51

标签: reactjs

我有此代码:

const fetchRequest = useCallback( () => {
        setIsLoading( true );
        axios( {
            method: "GET",
            url: "https://bouvet.guru/rekrytering_flera.php"
        } ).then( ( response ) => {
            if ( response.data.indexOf( "https://" ) !== -1 ) {
                let splittedArray = response.data.split( "Data:" );
                let dataString = splittedArray[1];

                if ( dataString.includes( "<URL kommentar>" ) ) {

                    dataString.substring( 16 );
                    let pairedData = dataString.split( "http" );
                    pairedData.splice( 0, 1 );

                    const finalArray = pairedData.map( ( group ) => {
                        const url = group.split( " " )[0];
                        const comment = group.replace( url, "" );

                        // Return an object with the paired properties. ID is not optimal to be Math.random(), this is for demonstration purpose.
                        return { url: "http" + url, comment, id: Math.random() };

                    } );

                    setData( [ ...data, ...finalArray ] );
                    setIsLoading( false );
                } else {
                    console.log( "NOPE" );
                }
            } else {
                console.log( "We could not find any data" );
                setIsLoading( false );
            }
        } );
    }, [] );

    useEffect( () => {
        fetchRequest();
        setInterval( () => {
            fetchRequest();
        }, 30000 );
    }, [] );


    console.log( data );

这只是一个简单的useEffect axios调用,在其中我收到一个字符串,然后将其设置为state,然后将其记录到控制台中。

我总是得到2个相同的数组。

(我知道它现在是一个useCallback,但是在整个代码中它只是一个useEffect时,也会遇到相同的问题。

此组件是独立的。

2 个答案:

答案 0 :(得分:1)

您可以这样做:

const fetchRequest = () => {
        setIsLoading( true );
        axios( {
            method: "GET",
            url: "https://bouvet.guru/rekrytering_flera.php"
        } ).then( ( response ) => {
            if ( response.data.indexOf( "https://" ) !== -1 ) {
                let splittedArray = response.data.split( "Data:" );
                let dataString = splittedArray[1];

                if ( dataString.includes( "<URL kommentar>" ) ) {

                    dataString.substring( 16 );
                    let pairedData = dataString.split( "http" );
                    pairedData.splice( 0, 1 );

                    const finalArray = pairedData.map( ( group ) => {
                        const url = group.split( " " )[0];
                        const comment = group.replace( url, "" );

                        // Return an object with the paired properties. ID is not optimal to be Math.random(), this is for demonstration purpose.
                        return { url: "http" + url, comment, id: Math.random() };

                    } );

                    setData( [ ...data, ...finalArray ] );
                    setIsLoading( false );
                } else {
                    console.log( "NOPE" );
                }
            } else {
                console.log( "We could not find any data" );
                setIsLoading( false );
            }
        } );
    };

    useEffect( () => {
        fetchRequest();
        const interval = setInterval( () => {
            fetchRequest();
        }, 30000 );
        return ()=>{clearInterval(interval);}
    }, [] );


    console.log( data );

答案 1 :(得分:0)

第二次渲染很有可能是由setIsLoading( false )

触发的