错误:使用react-hooks

时间:2019-12-02 03:14:21

标签: reactjs firebase google-cloud-firestore react-hooks

我正在尝试更改所有源代码,从使用对redux进行反应到与redux进行钩子连接并将其与firebase连接:

当我重定向到localhost:3000 / blogs时,会发生此警告

index.js:1375 Warning: Maximum update depth exceeded. This can happen when a 
component calls 
setState inside useEffect, but useEffect either doesn't have a dependency 
array, or one of the 
dependencies changes on every render.
in BlogDashboard (created by Context.Consumer)
in Route (at App.jsx:32)
in Switch (at App.jsx:31)
in div (created by Container)
in Container (at App.jsx:30)
in Route (at App.jsx:27)
in App (at src/index.js:44)
in Router (created by BrowserRouter)
in BrowserRouter (at src/index.js:40)
in ReduxFirestoreProvider (created by ReactReduxFirebaseProvider)
in ReactReduxFirebaseProvider (at src/index.js:39)
in Provider (at src/index.js:38)

在Index.js中:

ReactDOM.render(
    <Provider store={store}>
         <ReactReduxFirebaseProvider {...rrfProps}> // firebase worked fine
             <BrowserRouter>
                    <ReduxToastr timeOut={2000} preventDuplicates position="bottom-right" 
                                transitionIn="fadeIn" transitionOut="fadeOut" />
                    <App />
             </BrowserRouter>
         </ReactReduxFirebaseProvider>
     </Provider>, 
     document.getElementById('root');
);

在App.jsx中:

const App = () => {
const auth = useSelector(state => state.firebase.auth, []);
if(!auth.isLoaded && auth.isEmpty) return <LoadingComponent />

return (
    <Fragment>
        <ModalManager />
        <Switch>
            <Route exact path='/' component={HomePage}/>
        </Switch>
        <Route path='/(.+)' render={() => (
            <Fragment>
                <NavBar />
                <Container className="main">
                    <Switch>
                        <Route exact path='/blogs' component={BlogDashboard} />
                        <Route path='/blogs/:id' component={BlogDetailedPage} />
                    </Switch>
                </Container>
            </Fragment>
            )} 
        />
    </Fragment>
    );
}

在BlogDashboard.jsx中:

const BlogDashboard = () => {
const dispatch = useDispatch();
const firestore = useFirestore();

const blogs = useSelector(state => CastToArray(state.firestore.data.blogs) || []);//this worked fine
const moreBlogs = useSelector(state => state.blogs.moreBlogs);
const loading = useSelector(state => state.async.loading);

useEffect(() => {
    const getBlogs = async () => {
        await dispatch(getPagedBlogs({firestore}));
    };

    if(blogs.length === 0) {
        getBlogs();
    } else {
    }
}, [dispatch, firestore, blogs]);

const handleGetNextBlogs = async () => {
    await dispatch(getPagedBlogs({firestore}));
};


return (
    <Grid>
        <Grid.Column width={10}>
            <BlogList blogs={blogs} loading={loading} moreBlogs ={moreBlogs} getNextBlogs= 
                                                                         {handleGetNextBlogs} /> 

        </Grid.Column>
        <Grid.Column width={10}>
            <Loader active={loading} />
        </Grid.Column>
    </Grid>
    ); 
};

export default BlogDashboard;

在BlogActions中:

export const getPagedBlogs = ({firestore}) => 
async (dispatch, getState) => {
    dispatch(StartAction());
    const LIMIT = 5;
    let nextBlog = null;

    const {firestore: {data: {blogs: items}}} = getState();

    if (items && Object.keys(items).length >= LIMIT) {
        let itemsArray = objectToArray(items);
        nextBlog = await firestore.collection('blogs').doc(itemsArray[itemsArray.length - 
         1].id).get();
    }

    let query = await firestore.get({
        collection: 'blogs',
        limit: LIMIT,
        where: ['date', '<=', new Date()],
        startAfter: nextBlog,
        storeAs: 'blogs'
    });

    if(query.docs.length < LIMIT) {
        dispatch({type: MORE_BLOGS});
    }
    dispatch(FinishAction());
};

在BlogReducer.js中:

const initialState =  {
    blogs: [],
    moreBlogs: true,
};
export const moreBlogs = (state) => {
    return {
        ...state.blogs,
        moreBlogs: true
    }
}

export default createReducer(initialState, {
    [MORE_BLOGS]: moreBlogs
});

在BlogList.jsx中:

const BlogList = ({blogs, moreBlogs, getNextBlogs, loading}) => 
<Fragment>
    {blogs && blogs.length !== 0 && 
         //Load blogs, using 'react-scroll-infinite'.
        <InfiniteScroll pageStart={0} loadMore={getNextBlogs} hasMore= 
  {!loading && moreBlogs} initialLoad={false}> 
            {blogs && blogs.map(blog => (
                <BlogListItem key={blog.id} blog={blog} />
            ))}
        </InfiniteScroll>
    }
</Fragment>

export default BlogList;

我了解警告的意思,但是对React Hook的经验不足。

1 个答案:

答案 0 :(得分:2)

您提供的代码中没有一个显示您正在使用Firestore数据并将其设置为State的位置。使用setState将数据实际存储在您的React应用中。