简体   繁体   English

当上下文状态改变时,React 钩子上下文组件不会重新渲染

[英]React hooks context component doesn't re-render when context state changes

I am working on a react app that should allow a user to browsers records from a database one page at a time.我正在开发一个反应应用程序,它应该允许用户一次一页地浏览数据库中的记录。

I am using functional components and react context to manage state.我正在使用功能组件和反应上下文来管理状态。 I have a useEffect that will pulls a function from my Context to fetch records from the database and puts them into the Context State.我有一个 useEffect ,它将从我的上下文中提取一个函数来从数据库中获取记录并将它们放入上下文状态。 The component pulls the array of records from the Context State and if they is at least 1 (array length > 0) it will map the array using each record to populate a record list item component.该组件从上下文状态中提取记录数组,如果它们至少为 1(数组长度 > 0),它将使用每个记录映射数组以填充记录列表项组件。

The data is pulled and the React Dev Tools shows that the data is in the Context.数据被拉取,React Dev Tools 显示数据在 Context 中。 I can console log the records array (with records) to the console and I can even log the list of我可以将记录数组(带有记录)控制台记录到控制台,我什至可以记录

  • elements that are the Record List Item Components to the console, but the page always show the "No Records Found" message that you should get if the array is empty (length of 0).元素是控制台的记录列表项组件,但页面始终显示“未找到记录”消息,如果数组为空(长度为 0),您应该获得该消息。

    The main component is:主要组成部分是:

     <ul className="list-group"> { records.length > 0 ? (records.map((rec) => <RecordListItem record={rec}/>)) : (<li className="list-group-item">No Records Found</li>) } </ul>

    the useEffect in the RecordsList is: RecordsList 中的 useEffect 是:

     const { filter, getRecords, page, perPage, records, } = useContext(RecordsContext); useEffect(() => { const start = (page - 1) * perPage; getRecords(start, perPage, filter); // filter, page, and perPage come from the Context }, []); // only want this useEffect to run once when the component loads.

    getRecords is a function that queries the database for records starting from one record and going up to the number of records per page. getRecords 是一个函数,它在数据库中查询从一条记录开始到每页记录数的记录。

     const getRecords = async (start, limit, filter) => { if (filter) { const items = await db("contacts").select() .where({status: filter}) .offset(start).limit(limit); } else { const items = await db("contacts").select() .offset(start).limit(limit); } dispatch({type: SET_RECORDS, payload: items);

    The records are showing up in the dev tools and in the console logs.记录显示在开发工具和控制台日志中。 I am not seeing a problem with the database connection or queries.我没有看到数据库连接或查询有问题。

    The only problem I am seeing is that the component doesn't update when the state inside the context changes.我看到的唯一问题是,当上下文中的状态发生变化时,组件不会更新。 This is only happening with this one element.这只发生在这一个元素上。 Other state values are changing and the components that use them are updating.其他状态值正在变化,使用它们的组件也在更新。

    For example.例如。 When the filter is picked the number of records updates to reflect the total number of records with that filter.选择过滤器后,记录数会更新以反映该过滤器的记录总数。 Adding, updating, and deleting records in the database also cause these numbers to update when the components are refreshed.在数据库中添加、更新和删除记录也会导致这些数字在刷新组件时更新。

    I have another useEffect that will trigger when the page or perPage changes and another that resets the page to 1 and getsRecords again when the filter changes.我有另一个 useEffect 将在页面或 perPage 更改时触发,另一个将页面重置为 1 并在过滤器更改时再次getsRecords。 Both of these useEffects are currently not in the code to avoid them causing issues.这两个 useEffects 目前都没有在代码中,以避免它们引起问题。

    The Parent Component is the RecordsViewer.父组件是 RecordsViewer。 This is simply just a shell to hold the other elements in. It creates a Navbar at the top that gives filter options as well as paging and items per page options and a section with a container这只是一个用来容纳其他元素的外壳。它在顶部创建一个导航栏,提供过滤器选项以及分页和每页项目选项以及带有容器的部分

    The RecordsList is a card that contains the List-Group element and gets the data from the Context. RecordsList 是一张卡片,其中包含 List-Group 元素并从上下文中获取数据。 It then maps over the fetched records and passes each record to a display component.然后它映射获取的记录并将每个记录传递给显示组件。

    The RecordsContextProvider wraps the RecordsViewer and I can see the data in the Context within the React Dev Tools. RecordsContextProvider 包装了 RecordsViewer,我可以在 React Dev Tools 中查看 Context 中的数据。 Also doing a console.log(records) does show an array of the correct number of items.也做一个console.log(records)确实显示了正确数量的项目的数组。 I have tried capturing the map of records in a value and then showing that, but no change.我试过在一个值中捕获记录映射,然后显示它,但没有变化。 I can console.log that variable and see it is an array of react elements of type "LI".我可以使用 console.log 该变量并查看它是一个“LI”类型的反应元素数组。

    I am completely lost as to what the hang up is here.我完全不知道这里的挂断是什么。

  • I've found the issue.我找到了问题所在。 Misspelled length as lenght in the condition to check if the array was empty or not.拼写错误的长度作为条件中的长度以检查数组是否为空。

    What I get for trying to work after less than 5 hours of sleep.我在不到 5 小时的睡眠后尝试工作的结果。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM