简体   繁体   English

我如何访问withStyle组件中的DOM节点?

[英]How I can access to DOM node inside of withStyle component?

I'm working on react project and in this project my colleagues and I are using Material UI , for some reason I wanna access to DOM node of another component that wrapped by a HOC . 我正在研究react项目,在这个项目中我的同事和我正在使用Material UI ,出于某种原因,我想访问另一个由HOC包装的组件的DOM node I used react ref for this. 我使用了反应ref but I just get the withStyle object, see below: 但我只是得到了withStyle对象,见下文:

This is my TableHead : 这是我的TableHead

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import { TableHead as MaterialTableHead, TableRow } from '@material-ui/core';
import TableCell from './TableCell';

const TableHead = ({ classes, head, ...rest }) => (
  <MaterialTableHead {...rest}>
    <TableRow>
      {head.map(item => (
        <TableCell key={item.key} className={classes.headCell} style={{ width: item.width }}>
          {item.title}
        </TableCell>
      ))}
    </TableRow>
  </MaterialTableHead>
);

TableHead.propTypes = {
  classes: PropTypes.object.isRequired,
  head: PropTypes.arrayOf(PropTypes.shape({
    key: PropTypes.string.isRequired,
    title: PropTypes.string.isRequired,
    width: PropTypes.string,
    render: PropTypes.func,
  })).isRequired,
};

TableHead.defaultProps = {};

const styles = () => ({
  headCell: {
    fontSize: '18px',
    color: '#0c1d38',
  },
});

export default withStyles(styles, { withTheme: true })(TableHead);

And in the Table component I wanna calculate height of TableHead component, so I use below code: Table组件中我想计算TableHead组件的高度,所以我使用下面的代码:

<TableHead ref={(header) => { this.header = header; }} head={head} />

and inside of componentDidMount of Table component I console.log(this.header) and just see below: Table组件I的console.log(this.header)componentDidMount ,如下所示:

enter image description here 在此输入图像描述

I seek in the web and find some GitHub issues pages and try innerRef instead of ref but it doesn't help me. 我在网上搜索并找到一些GitHub问题页面并尝试使用innerRef而不是ref但它对我没有帮助。

How I can access to DOM node to calculate its height? 我如何访问DOM节点来计算它的高度?

What you are looking for is innerRef property. 您正在寻找的是innerRef属性。 Just replace ref with innerRef . 只需用innerRef替换ref

Example: 例:

<TableHead innerRef={(header) => { this.header = header; }} head={head} />

Source ( withStyles docs): 来源( withStyles docs):

Some implementation details that might be interesting to being aware of: 一些可能有趣的实现细节:

It adds a classes property so you can override the injected class names from the outside. 它添加了一个classes属性,因此您可以从外部覆盖注入的类名。

It adds an innerRef property so you can get a reference to the wrapped component. 它添加了一个innerRef属性,因此您可以获得对包装组件的引用。 The usage of innerRef is identical to ref. innerRef的用法与ref相同。

It forwards non React static properties so this HOC is more "transparent". 它转发非React静态属性,因此这个HOC更“透明”。 For instance, it can be used to defined a getInitialProps() static method (next.js). 例如,它可用于定义getInitialProps()静态方法(next.js)。

Reference: https://material-ui.com/customization/css-in-js/#withstyles-styles---options----higher-order-component 参考: https//material-ui.com/customization/css-in-js/#withstyles-styles---options----higher-order-component

I know it's late, but for other people which might face this problem I provide my solution, too. 我知道现在已经很晚了,但对于可能面临这个问题的其他人,我也提供了解决方案。 Material UI has a RootRef API which you can use a as HOC the case you want to get the element DOM node: Material UI有一个RootRef API ,您可以在需要获取元素DOM节点的情况下使用HOC作为HOC:

First import the component: 首先导入组件:

import React from 'react';
import RootRef from '@material-ui/core/RootRef';

Then wrap your entire element inside a RootRef component and create a React ref. 然后将整个元素包装在RootRef组件中并创建一个React引用。 But instead of adding a ref on your component and reference it to the ref like this ref={this.tableHeadRef} , you should add rootRef to the rootRef HOC and reference it to your ref like this: rootRef={this.tableHeadRef} to get the current DOM node. 但是不要在你的组件上添加引用并将它引用到引用,例如ref={this.tableHeadRef} ,你应该将rootRef添加到rootRef HOC并将它引用到你的ref中,如下所示: rootRef={this.tableHeadRef} to获取当前的DOM节点。 This can be applied to any component that uses withStyles HOC. 这可以应用于使用withStyles HOC的任何组件。

class MyComponent extends React.Component {
  constructor() {
    super();
    this.tableHeadRef = React.createRef();
  }

  componentDidMount() {
    console.log(this.tableHeadRef.current); // DOM node
  }

  render() {
    return (
      <RootRef rootRef={this.tableHeadRef}>
        <TableHead />
      </RootRef>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM