【问题标题】:how to add attribute and add class in react js?react js中如何添加属性和添加类?
【发布时间】:2021-09-13 08:29:47
【问题描述】:

我想在 react js 中添加属性和类。我正在尝试使用 react 创建TABS。当Tab 元素宽度大于45px 和新属性@ 时,我想添加hidden 类987654326@ 的值为 true 。 你能告诉我怎么做吗?

组件

<Tabs>
      {data.map((i, index) => (
        <li key={index}>{i}</li>
      ))}
    </Tabs>

Tabs.js

 const getTabs = () => {
    console.log("get tabs");
    const { blockWidth, tabsTotalWidth, tabDimensions, showMoreWidth } = state;

    let tabIndex = 0;
    let availableWidth = blockWidth - showMoreWidth;
    return children.reduce(
      (result, tabItem, index, arr) => {
        const { key = index } = tabItem.props;
        const tabWidth = tabDimensions[key] ? tabDimensions[key].width : 0;

        if (tabWidth < 45) {
          //  aria-hidden= false
          // remove hidden class tabItem if present.
          result.tabsVisible.push(tabItem);
        } else {
          //  aria-hidden= true
          // add hidden class tabItem
          result.tabsHidden.push(tabItem);
        }
        /* eslint-enable no-param-reassign */

        availableWidth -= tabWidth;
        return result;
      },
      {
        tabsVisible: [],
        tabsHidden: []
      }
    );
  };

这是我的代码

https://codesandbox.io/s/reverent-hermann-yt7es?file=/src/tabs.js:2147-2486

我正在像这样渲染标签

return (
     <ul ref={tabsRef} className="rc64nav">
          {/* {children} */}
          {tabsVisible.reduce((result, tabItem, tabIndex) => {
            result.push(tabItem);
            return result;
          }, [])}
    
          {tabsHidden.reduce((result, tabItem, tabIndex) => {
            result.push(tabItem);
            return result;
          }, [])}
        </ul>

【问题讨论】:

    标签: javascript reactjs react-redux react-hooks


    【解决方案1】:

    对于类,您可以简单地添加 className 属性。您可以添加与 HTML 元素完全相同的属性。

    【讨论】:

    • 那你如何给一个 React 组件添加类呢?你能告诉我正确的答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 2020-06-14
    • 2021-09-26
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多