简体   繁体   English

如何在React-Router-Dom NavLink单击时强制重新渲染React-Redux组件?

[英]How to force a re-render of React-Redux components upon React-Router-Dom NavLink click?

I have a React application built using React-Router-Dom for navigation. 我有一个使用React-Router-Dom进行导航的React应用程序。 The pages are very heavy on use of tables of data that can be opened in a CRUD form. 这些页面非常注重使用可以以CRUD形式打开的数据表。 Due to the amount of records we have in the tables, it's not uncommon for the user to apply sorting, filtering, and paging to the tables in order to find a desired record. 由于表中有大量记录,因此用户对表进行排序,过滤和分页以查找所需记录的情况并不少见。

As a fairly standard web application, we have a navigation bar on the left-hand side of the page. 作为一个相当标准的Web应用程序,我们在页面的左侧有一个导航栏。 This is built up of NavLinks . 这是由NavLinks构建的

The main body of the application is a component, MainPanel , that is almost entirely just a series of Route components within a Switch : 该应用程序的主体是MainPanel组件,它几乎完全是Switch的一系列Route组件:

  <Switch>
    <Route path="/page1" component={ComponentOne} />
    <Route path="/page2" component={ComponentTwo} />
  </Switch>

I now have a request from above that when a user selects one of the NavLinks, it should act as a "reset" for everything in the main body of the page. 我现在从上面有一个要求,当用户选择其中一个NavLink时,它应该充当页面主体中所有内容的“重置”。 In particular, this should act as a "reset" for all the settings of the table (sort, filter, page, etc.). 特别是,这应该作为表的所有设置(排序,过滤器,页面等)的“重置”。

This seems like it should almost be trivial, as all I have to do is trigger a re-render of the main panel of the page holding the table. 似乎几乎应该是微不足道的,因为我要做的就是触发保存该表的页面的主面板的重新渲染。 However, I can't find anything in the documentation for React-Router-Dom that holds the key to this. 但是,我在React-Router-Dom的文档中找不到任何包含此内容的密钥。

Anybody got any idea what I'm missing? 有人知道我想念什么吗? Is there no easy/right way to say, "When clicking this link, always re-render the MainPanel? 没有简单/正确的方法说:“单击此链接时,请始终重新渲染MainPanel?

You could keep a value in state, eg called panelKey , that you use as key prop for the main panel. 您可以保持一个值处于状态,例如,称为panelKey ,用作主面板的key道具。 You then give panelKey a new value when the the link is clicked, and React will throw away the main panel and create an entirely new component. 然后,当单击链接时,您可以给panelKey一个新的值,React将丢弃主面板并创建一个全新的组件。

Example

 class Timer extends React.Component { timer = null; state = { count: 0 }; componentDidMount() { this.timer = setInterval(() => { this.setState(prevState => ({ count: prevState.count + 1 })); }, 1000); } componentWillUnmount() { clearInterval(this.timer); } render() { return <div>{this.state.count}</div>; } } class MainPanel extends React.Component { state = { panelKey: 1 }; onClick = () => { this.setState(prevState => ({ panelKey: prevState.panelKey + 1 })); }; render() { return ( <div key={this.state.panelKey}> <button onClick={this.onClick}> Reset Panel </button> <Timer /> </div> ); } } ReactDOM.render(<MainPanel />, document.getElementById("root")); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

I see you have listed react-redux in the tags, why not dispatch a custom action to notify other components that filters should be reset? 我看到您在标记中列出了react-redux,为什么不调度自定义操作来通知其他组件应该重置过滤器?

import { LOCATION_CHANGE } from 'react-router-redux';
import { resetFilter } from './filterActions.js';

const resetFilterMiddleware = ({ dispatch }) => next => action => {
    // Listen for a location chnage
    if(action.type === LOCATION_CHANGE){
        dispatch(resetFilter()); // { type: 'RESET_FILTER' } or something along the lines
    }

    next(action);
}

export default resetFilterMiddleware;

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

相关问题 如何使用带有 react-redux 的 react-router-dom v6.0.0 渲染不同的组件 - how to render different components using react-router-dom v6.0.0 with react-redux 如果视图在带有 react-router-dom 的 React 路由中,我如何取消重新渲染视图 - How can I cancel re-render view if view is in Route on React with react-router-dom 如何重新渲染一个完整的页面<link> react-router-dom 中的标签 - How to re-render a full page with <Link> tag in react-router-dom 重新渲染React和Redux中的所有组件 - Re-render all components in React and Redux 检测查询参数react-router-dom v4.x中的更改并重新呈现组件 - Detect change in query param react-router-dom v4.x and re-render component React-Router-Dom - 重定向不会导致重新渲染 - React-Router-Dom - redirecting doesn't cause re-render React-redux 代码不会在成功的 redux 调度和成功的减速器后重新渲染反应组件 - React-redux code does not drive a re-render of the react component upon a successful redux dispatch and successful reducer 如何从react-router-dom用Mocha测试`NavLink` - How to test `NavLink` with Mocha from react-router-dom 如何使用 react-router-dom 将 NavLink 设置为活动状态 - How to set a NavLink as active using react-router-dom 如何检查是否将完成的POST方法提交到数据库,然后在React-Redux中重新渲染 - How to check if submit finished POST method to database and then re-render in React-Redux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM