如何在不渲染子组件的情况下根据子组件的大小更改React父组件的大小?

尼基尔·帕蒂尔

我创建了一个React组件,该组件接受任何组件并将其呈现为弹出窗口。父组件接收要渲染(弹出)的组件。这里渲染的组件是子组件,它使用react-sizeme来获取其大小并传递回父组件。父组件必须具有子组件的尺寸,因此请调整其高度和宽度这是代码:

class Popup extends React.Component<IPopupProps,IComponent>{
    constructor(props:IPopupProps){
        super(props);
        this.state={
            childComponent:this.props.children,
            style:{
                height:0,
                width:0
            }
        }     
    }
    
    // This function runs two times, before and after rendering child component
    // & so have an improper visualization as the size is changed twice here
    
    public OnSize = (size:any) =>{
        const width = size.width +20;
        const height =size.height+20;
        this.setState({
            style:{height,
                width             }
        })
    }

    public render(){
        return(
            <div className='popup'>
                <div style={this.state.style} className='popup-content'>
                    <a className="close" onClick={this.props.onExit}>
                            &times;
                    </a>
                    <this.state.childComponent onSize={this.OnSize}/>                    
                </div>
            </div>
        )
    }
}

初始宽度和高度设置为0。因此无法正确渲染。那么有什么方法可以隐藏子组件或避免在父组件获得尺寸之前渲染它?

编辑:在子组件被渲染之前,我们无法获得大小。所以有什么技巧可以做到这一点。仅一个组件需要正确弹出。

编辑2:这是PropsBuilder.tsx,它调用Popup.tsx并发送该组件以显示为子代

class PopupBuilder extends React.Component<IPopupBuilderProps, IPopup>{
    constructor(props:IPopupBuilderProps){
        super(props);
        this.state = {
            showPopup:false
        }
    }

    public togglePopup = () =>{
        this.setState({
            showPopup:!this.state.showPopup
        })
    }

    public render (){
        return(
            <React.Fragment>

                    <button onClick={this.togglePopup}>{this.props.trigger}</button>

                    <React.Fragment>
                        {this.state.showPopup?<Popup onExit={this.togglePopup} >{this.props.component}</Popup>:null}
                    </React.Fragment>

            </React.Fragment>
            
        )
    }
}

export default PopupBuilder;

舍甫琴科·维克多

实际上,这看起来像是更一般的DOM / JavaScript问题。

考虑这种情况:

const span = document.createElement('span');
span.innerText = 'hello';
span.getBoundingClientRect() // -> { width: 0, height: 0, top: 0, … }

这表明您直到元素在DOM中才知道元素的尺寸(在react中渲染)。

document.body.appendChild(span);
span.getBoundingClientRect(); // -> {width: 50, height: 16,  …}

在这种情况下,我对您的建议是:

  1. 子组件应接受父组件的属性(功能)
  2. 使用React“ ref”功能查找子元素的实际尺寸
  3. 在“ componentDidMount”中调用函数(如果子组件可以动态更改,则使用componentDidUpdate),并向其传递子组件尺寸。

如果您无权访问子组件。您可以这样包装它:

// Popup.tsx

class Popup .... {
 ...
   render() {
     <Measurer>{this.props.children}</Measurer>
   }
}

并实现其中提取尺寸的逻辑。Measurer是Popup的直接子代,您可以控制它们的通信。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

根据父维度渲染 React 子组件

如何在React Native中的子组件上更改时重新渲染父组件?

在这种情况下如何重新渲染子组件?

Angular 2-如何在不两次渲染子组件的情况下将数据传递给子组件?

反应父组件不渲染子组件

Nuxt 父组件在子组件下渲染

React:更新子组件而不渲染父组件

如何在不键入JS子组件的情况下将未类型化的ES6 react组件导入Typescript组件?

如何在不更改状态的情况下重新渲染组件

next.js 如何在不触发按钮的情况下从子组件更改父状态?

Angular:如何根据父应用程序组件的路由更改重新渲染子组件?

如何在不指定组件类型的情况下将父组件注入共享指令?

在 Meteor+React 中,如何在父 React 组件中渲染子 React 组件?

如何在不更改组件状态的情况下更改组件状态的副本?

如何在父组件下加载子组件

角线不渲染子组件,而是渲染父组件

如何从 React 中的子组件更改父组件中的状态?

在重新渲染父组件时,React如何重用子组件/保持子组件的状态?

如何在父组件之外渲染子组件(DOM 层次结构) - React.js

子组件不渲染

当父状态在React中更改时停止渲染子组件

如何在不重新渲染所有子组件的情况下更新Material-bottom-tabs徽章文本?

如何在不重新渲染父组件的情况下正确获取鼠标事件

如何更改React组件的大小?

在父组件的状态更改时重新渲染子组件

如何根据设备大小渲染不同的组件?

如何在 React 中从父组件更改子组件的属性?

如何在vue.js中渲染父组件中的子组件,并且只渲染子组件中发生的变化?

我如何在不卸载的情况下渲染组件数组?