【问题标题】:React set an element with one className's min-height equal to the element with another className's min-heightReact 将具有一个类名的最小高度的元素设置为等于具有另一个类名的最小高度的元素
【发布时间】:2017-05-10 12:55:54
【问题描述】:

我有一个带有两个 <div> 标签的 React 组件,每个标签都有不同的 classNames 可以说:className="firstClass"className="secondClass"

如何将secondClassmin-height 值设置为等于firstClassmin-height 值?

【问题讨论】:

  • 如何设置firstClassmin-height值?
  • componentDidMount() 函数中我设置了`min-height'。

标签: css reactjs


【解决方案1】:

不要直接设置 DOM。您需要阅读setStateprops

setState 触发重新渲染,因此无论何时执行this.setState({minHeight: newValueHere});,它都会自动使用新值更新渲染方法。

class Component extends React.Component {
  constructor() {
    super();
            
    this.state = {
      minHeight: "600px"
    };
  }

  render() {
    return (
      <div>
        <div className="firstClass" style={{minHeight: this.state.minHeight}} />
        <div className="secondClass" style={{minHeight: this.state.minHeight}} />
      </div>
    );
  }
}

【讨论】:

    猜你喜欢
    • 2017-03-23
    • 1970-01-01
    • 2011-08-28
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多