【问题标题】:CSS changes on new line in react component反应组件中新行的CSS更改
【发布时间】:2017-05-25 16:58:26
【问题描述】:

我有一个 react 组件,其中包含内联块 div 项目,每个项目都有一个边框,以提供“分隔符”视觉效果。但是,如果它达到父组件的最大宽度,无论是通过初始加载还是进一步调整窗口大小,该行的最后一项都不应该有边框。它应该看起来像这样:

第 1 项 |项目 2 |项目 3 |第 4 项

第 5 项 |项目 6 |第 7 项

我已经阅读了有关使用 jquery 检测项目何时更改 offsetTop 值的信息,但我不确定它将如何与反应组件交互。任何指导表示赞赏。

【问题讨论】:

    标签: javascript jquery css reactjs


    【解决方案1】:

    你可以使用css:

    :nth-last-child {
        border-right-style: none;
    }
    

    【讨论】:

      【解决方案2】:

      好吧,这里的技巧是使用ref 来获取项目的左偏移量,如果项目的左偏移量是0,则将leftmost 类添加到它。计算是在组件安装后完成的(在componentDidMount 方法中)。

      此外,我添加了 version 属性,它在每次调整窗口大小时都会增加(去抖动以避免过度更新,如果需要,您可以降低超时或完全删除它)以在浏览器时强制重新计算边框状态窗口已调整大小。

      确保以全页模式运行演示,以查看调整浏览器窗口大小时会发生什么。

      class Item extends React.Component {
        constructor(props){
          super(props); 
          
          this.state = {
            isLeftMost: false
          };
          
          this.recalculateBorder = () => {
            if(this.el){
              this.setState({isLeftMost: this.el.offsetLeft === 0});
            }
          }
        }
        
        componentDidMount(){
          this.recalculateBorder();
        }
        
        componentDidUpdate(){
          this.recalculateBorder();
        }
        
        shouldComponentUpdate(nextProps, nextState){
          return (nextProps.label !== this.props.label)
              || (nextProps.version !== this.props.version)
              || (nextState.isLeftMost !== this.state.isLeftMost);
        }
        
        render(){
          let cl = this.state.isLeftMost ? "item leftmost" : "item";
          return (
            <div className={cl} ref={el => this.el = el}>{this.props.label}</div>
          );
        }
      }
      
      class Container extends React.Component {
        constructor(props){
          super(props);
          this.state = { version: 0 };
          
          let _updateDimensions = () => {
            this.setState({version: this.state.version+1 }); 
          }
          
          this.updateDimensions = _.debounce(_updateDimensions, 25);
        }
        
        componentDidMount() {
           window.addEventListener("resize", this.updateDimensions);
        }
      
        componentWillUnmount() {
           window.removeEventListener("resize", this.updateDimensions);
        }
        
        render(){
          let items = [];
          for(let i = 0; i<30; i++){ 
            items.push(<Item key={i} label={"item " + i} version={this.state.version} />);
          }
          return (
            <div className="container">
              {items}
            </div>
          );
        }
      }
        
      ReactDOM.render(<Container />, document.getElementById("root"));
      .container {
        position: relative;
      }
      
      .item {
        display: inline-block;
        padding: 0 5px;
        border-left: solid 2px red;
       }
      
      .item.leftmost {
        border-left-color: transparent;
      }
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
      
      <div id="root"></div>

      【讨论】:

      • 难以置信!如此全面的解决方案和对 ref 属性的精彩介绍。非常感谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 2021-10-08
      相关资源
      最近更新 更多