相关疑难解决方法(0)

React Styled Components - 如何访问原始html

我有一个组件的参考我将转换为我的应用程序中的样式组件.ref用于访问组件的原始html元素上的offsetHeight和scrollHeight属性.一旦我将这个组件切换到样式组件,ref现在指向样式组件而不是原始html元素,我不确定如何引用基本元素.可以这样做吗?

例:

const TextArea = styled.textarea`
  display: block;
  margin: 0 0 0 18%;
  padding: 4px 6px;
  width: 64%;
  font-size: 1rem;
  color: #111;`;

export default class Input extends Component {
  componentDidMount() {
    const height = this.textInput.scrollHeight;
    // do something....
  }
  render() {
    return (
      <div>
        <TextArea
          ref={(input) => this.textInput = input}
        ></TextArea>
      </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript css reactjs styled-components

6
推荐指数
1
解决办法
3631
查看次数

标签 统计

css ×1

javascript ×1

reactjs ×1

styled-components ×1