【问题标题】:Material-UI to new hook Reactjs?Material-UI 到新的钩子 Reactjs?
【发布时间】:2019-11-21 07:53:07
【问题描述】:

所有关于如何制作 reactJS 网站的新示例都显示了类似的示例

const NavBar = (params) => {
    return(
        <div>
        text
        </div>
    )
}
export default NavBar;

然而,material-UI 似乎显示了以下内容,这让我很难理解如何让选项卡与上面的新代码格式一起工作。构造函数在新格式下如何工作?

import React from 'react';
import {Tabs, Tab} from 'material-ui/Tabs';

const styles = {
  headline: {
    fontSize: 24,
    paddingTop: 16,
    marginBottom: 12,
    fontWeight: 400,
  },
};

export default class TabsExampleControlled extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      value: 'a',
    };
  }

  handleChange = (value) => {
    this.setState({
      value: value,
    });
  };

  render() {
    return (
      <Tabs
        value={this.state.value}
        onChange={this.handleChange}
      >
        <Tab label="Tab A" value="a">
          <div>
            <h2 style={styles.headline}>Controllable Tab A</h2>
            <p>
              Tabs are also controllable if you want to programmatically pass them their values.
              This allows for more functionality in Tabs such as not
              having any Tab selected or assigning them different values.
            </p>
          </div>
        </Tab>
        <Tab label="Tab B" value="b">
          <div>
            <h2 style={styles.headline}>Controllable Tab B</h2>
            <p>
              This is another example of a controllable tab. Remember, if you
              use controllable Tabs, you need to give all of your tabs values or else
              you wont be able to select them.
            </p>
          </div>
        </Tab>
      </Tabs>
    );
  }
}

编辑

我找到了我需要做的事情 通过使用 useState,我可以检查用户在哪个页面上,并确保这是用户第一次进入该站点,然后设置一个 const,然后允许我运行另一个 const 来设置需要处于活动状态的选项卡。

const firstTime = false;       
React.useState(() => {
          if(!firstTime && window.location.pathname === "/two"){
            //console.log('mounted or updated')
            firstTime = 1;
          }
        }
      );


    const [value, setValue] = React.useState(firstTime);

【问题讨论】:

  • 什么是“新格式”?你是说上课吗?这不是新的。你的问题不清楚
  • 您的问题是关于功能组件的吗?如何在功能组件中使用状态?

标签: reactjs material-ui


【解决方案1】:

来自 MDN:

“构造函数方法是一种特殊的方法,用于创建和初始化在类中创建的对象。”

material-ui 示例是类组件,新示例是函数组件。函数组件不使用构造函数。

直到最近,当 Hooks API 被引入时,如果一个组件需要使用 State,它必须是一个类组件,但是有了 Hooks,现在可以在函数组件内部操作状态。我建议您阅读此内容。这是一个很好的开端: https://overreacted.io/how-are-function-components-different-from-classes/

以下是使用函数组件和钩子的重构版本:

import React, { useState } from "react";
import ReactDOM from "react-dom";
import { Tabs, Tab } from "@material-ui/core";

const styles = {
  headline: {
    fontSize: 24,
    paddingTop: 16,
    marginBottom: 12,
    fontWeight: 400
  }
};

const TabsExampleControlled = props => {
  const [value, setValue] = useState("a");

  return (
    <Tabs value={value} onChange={(event, value) => setValue(value)}>
      <Tab label="Tab A" value="a">
        <div>
          <h2 style={styles.headline}>Controllable Tab A</h2>
          <p>
            Tabs are also controllable if you want to programmatically pass them
            their values. This allows for more functionality in Tabs such as not
            having any Tab selected or assigning them different values.
          </p>
        </div>
      </Tab>
      <Tab label="Tab B" value="b">
        <div>
          <h2 style={styles.headline}>Controllable Tab B</h2>
          <p>
            This is another example of a controllable tab. Remember, if you use
            controllable Tabs, you need to give all of your tabs values or else
            you wont be able to select them.
          </p>
        </div>
      </Tab>
    </Tabs>
  );
};

const rootElement = document.getElementById("root");
ReactDOM.render(<TabsExampleControlled />, rootElement);

在这里查看https://codesandbox.io/s/material-ui-tabs-hooks-wkyzq

【讨论】:

  • 所以material-UI标签不能是函数?
  • 当然可以。我在答案中添加了一个示例。
猜你喜欢
  • 2021-09-10
  • 2020-02-16
  • 1970-01-01
  • 2021-06-08
  • 2020-03-12
  • 2022-01-24
  • 2019-10-23
  • 2021-11-12
  • 2019-11-24
相关资源
最近更新 更多