[英]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.