根据当前页面动态加载应用栏工具栏 - React Admin



我对 React 世界相对较新,正在努力找到一种方法将 IconButton 加载到 AppBar 的工具栏(从 Material UI(。

假设我在页面 A 上,并且喜欢有两个特定于页面 A 的 IconButton,当我到达页面 B 时,我喜欢加载特定于页面 B 的工具栏按钮。

我创建了一个代码沙箱项目,以便任何人都可以更轻松地提供帮助。 https://codesandbox.io/s/material-ui--react-admin-1qc5x

我感谢任何帮助,指导等。

您可以通过多种方式实现此目的:

  1. Redux:您可以使用react-redux并创建一个store,您可以在其中存储您的current_page值(SPA 的最佳实践(并访问页面值

    static getDerivedStateFromProps(props, state){ //your code here }

  2. 基于路线(react-router(
  3. [我所做的]基于URL params,就像您正在做的那样history.push因此更容易从您的URL中提取参数并基于此呈现您的工具栏图标。

我的代码参考:https://codesandbox.io/s/material-ui--react-admin-nxh7m

最新更新