【问题标题】:Fluent UI React commandbar usestate not possibleFluent UI React 命令栏使用状态不可能
【发布时间】:2020-11-04 22:40:52
【问题描述】:

我是 Fluent UI React 组件的新手。我正在尝试在命令栏控件上实现 React Router,来自流利的 UI 反应发现 here 它是带有溢出菜单项的命令栏。如果我想使用菜单项导航到不同的页面,我使用 history.push("/myLink") 解释here。但为了使该工作正常进行,我需要访问功能组件中的 useState 。代码如下所示:

        export const CommandBarBasicExample: React.FunctionComponent = () => {
        const [refreshPage, setRefreshPage] = useState(false);
      return (
        <div>
          <CommandBar
            items={_items}
            overflowItems={_overflowItems}
            overflowButtonProps={overflowProps}
            farItems={_farItems}
            ariaLabel="Use left and right arrow keys to navigate between commands"
          />
        </div>
      );
    };

    const _items: ICommandBarItemProps[] = [
      {
        key: 'newItem',
        text: 'New',
        cacheKey: 'myCacheKey', // changing this key will invalidate this item's cache
        iconProps: { iconName: 'Add' },
        subMenuProps: {
          items: [
            {  //first item in the menu
        key: "AddProperty",
        text: "Properties",
        iconProps: { iconName: "Add" },
        ["data-automation-id"]: "newProperty", // optional
        onClick: ()=>{handleclick()
        setRefreshPage(true);
        };
            {
              key: 'calendarEvent',
              text: 'Calendar event',
              iconProps: { iconName: 'Calendar' },
            },
          ],
        },
      },

我遇到的问题是,如果我使用 setRefreshPage(true) VS 代码抱怨状态变量无法识别。如果我将 useState 放在其他地方,则对非法使用 useState 的投诉做出反应。如何让 useState 在 const _items 对象中可用? 任何帮助将不胜感激。

【问题讨论】:

  • 你为什么不用哈希路由器?
  • 嗨,我正在使用 Hash 路由器,但它仍然无法正常工作 :-(.
  • 你能解释一下这个问题 - 你只是想在命令栏按钮点击上导航吗?
  • 确实!如果我单击命令栏项,我想导航到属性组件。该组件包含我的业务逻辑。但是当我单击按钮时,什么也没有发生。我使用 Create react app 和 typescript 重建应用程序,以查看它在浏览器中的行为。如果我使用 BrowserRouter 并使用 history.push("/Properties") 它会更改页面顶部的 URL,但不会刷新。因此我需要触发一个 useState 但我无法访问 _items [] 对象中的 useState。我希望这是有道理的?
  • 为什么你需要推送到历史?在那个表面上,您无论如何都无法访问后退按钮?

标签: reactjs office-js fluent-ui


【解决方案1】:

这是使用相同命令栏组件对我有用的内容。

您必须确保您的路由器设置为HashRouter,并且您的&lt;Route/&gt; 的路径属性通过按钮的href 属性设置为/#properties - 而不是通过onClick

我们有描述路线的路线文件:

/* routes.js */
export const Routes = {
   Properties: 'properties'
}

我们有这个文件,描述命令栏的内容。

/* commandBarItems.js */

import Routes from './routes'
// IMPORTANT - CHECK OUT THE HREF PROP
const PropertiesButton = { key: Routes.Properties, name: 'Properties', href: `#${Routes.Properties}` };

export const CommandBarItems = { menu: [PropertiesButton] }

我们有app.js,您可以在其中设置哈希路由器和命令栏组件。

/* app.js */
import React, { Component } from 'react';
import { HashRouter as Router, Route } from "react-router-dom";
import { Fabric, initializeIcons, CommandBar } from 'office-ui-fabric-react';
import { PropertiesComponent } from './whichever-file-or-module';
import Routes from './routes';
import CommandBarItems from './commandBarItems';

class App extends Component {
    constructor() {
        super();
        initializeIcons();
        ...
    }

    render() {
        return (
          <div>
            <Fabric>
              <Router>
                <React.Fragment>
                  <CommandBar items={CommandBarItems.menu} farItems={CommandBarItems.farItems}/>
                  <Route path={`/${Routes.Properties}`} component={PropertiesComponent} />
                </React.Fragment>
              </Router>
             </Fabric>
            </div>
          );
     }
}

【讨论】:

  • 嗨,马维。抱歉耽搁了。我有一些假期。我刚回来工作。你是个天才!我只需要使用 href 属性和路由页面!链接:#${Routes.Properties}!非常感谢!
猜你喜欢
  • 2015-10-10
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 2016-07-11
  • 2022-10-13
  • 2022-11-08
  • 1970-01-01
  • 2022-07-04
相关资源
最近更新 更多