简体   繁体   English

在React.js上进行条件渲染

[英]Conditional rendering on React.js

render() {
        const tableStyle = this.getTableStyle();
        const tableSettings = this.getTableSettings();

        return (
            <div style={tables}>

                <TablePosition
                    contextMenuOn={true}
                    step={this.props.step}
                    pdfData={this.props.pdfData}
                    tableSettings={tableSettings}
                    tableStyle={tableStyle}
                    fileName={this.state.fileName}
                    tableSize={this.getTableSize()}
                    tableOffset={this.state.tableOffset}
                    desiredWidth={700}
                    updateXOffset={x => this.updateXOffset(x)}
                    updateYOffset={y => this.updateYOffset(y)}
                    markTable={() => this.markTable()}
                    setOutputLabels={(row, col, val) => this.setOuputLabels(row, col, val)}
                />
            </div>
        );

        if (!this.props.isThirdStep) {
            return (
                <div>
                    <div style={sideBySide}>
                        <PDFViewer
                            isThirdStep={this.props.isThirdStep}
                            paginationCallback={this.handlePageChange}
                            pdfData={this.state.pdfData}
                            desiredWidth={600}
                            selectedPage={this.props.savedPageNo}
                        />
                    </div>
                </div>
            );     
        } else {
            return (
                <div>
                    <ReferenceMenu />
                </div>
            );     
        }
    }

In my component's render, I try to render several components based on certain conditions. 在组件的渲染中,我尝试根据某些条件渲染多个组件。

So, basically, the TablePoisition always stays there, and the PDFViewer and ReferenceMenu renders conditionally. 因此,基本上, TablePoisition始终停留在该位置,而PDFViewerReferenceMenu有条件地呈现。

However, what I see on both conditions is only the TablePosition component. 但是,在这两种情况下,我看到的只是TablePosition组件。

Is this not supposed to work? 这不行吗?

You need to place your conditional renders inside variables or something similar. 您需要将条件渲染放置在变量或类似内容内。

var conditionContent1 = null;
var conditionContent2 = null;

if(condition1){
    conditionContent1 = <div>conditional content 1</div>;
}

if(condition2){
    conditionContent2 = <div>conditional content 2</div>;
}

return (
    <div id="wrapper">
        <div>
            content
        </div>
        {conditionContent1}
        {conditionContent2}
    </div>
);

I added a wrapper div ; 我添加了一个包装器div because, I believe render 's return doesn't like having multiple root elements. 因为,我相信renderreturn不喜欢具有多个根元素。

If the variables are null; 如果变量为空; then, it won't affect the overall render. 然后,它不会影响整体渲染。

As explained since you want to combine two components you should change your render logic. 如前所述,由于要合并两个组件,因此应更改渲染逻辑。 One component will be sit there always and the other one will be rendered conditionally. 一个组件将始终坐在那里,而另一个组件将有条件地渲染。 So, you need to render that last component with the sticky one in the same return. 因此,您需要在同一返回中使用粘性组件来渲染最后一个组件。 I would do something like this: 我会做这样的事情:

renderPDFViewer = () => (
    <div>
        <div style={sideBySide}>
            <PDFViewer
                isThirdStep={this.props.isThirdStep}
                paginationCallback={this.handlePageChange}
                pdfData={this.state.pdfData}
                desiredWidth={600}
                selectedPage={this.props.savedPageNo}
            />
        </div>
    </div>
);

render() {
        const tableStyle = this.getTableStyle();
        const tableSettings = this.getTableSettings();

        return (
            <div>
            <div style={tables}>

                <TablePosition
                    contextMenuOn={true}
                    step={this.props.step}
                    pdfData={this.props.pdfData}
                    tableSettings={tableSettings}
                    tableStyle={tableStyle}
                    fileName={this.state.fileName}
                    tableSize={this.getTableSize()}
                    tableOffset={this.state.tableOffset}
                    desiredWidth={700}
                    updateXOffset={x => this.updateXOffset(x)}
                    updateYOffset={y => this.updateYOffset(y)}
                    markTable={() => this.markTable()}
                    setOutputLabels={(row, col, val) => this.setOuputLabels(row, col, val)}
                />
                </div>

        {
            !this.props.isThirdStep
                ? this.renderPDFViewer()
                : ( <div><ReferenceMenu /></div> )
        }
            </div>
        );
    }

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

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