如何在React中构建专用组件

问题描述 投票:0回答:2

我发现自己编写了这样的代码,对于一些具有一些共性的组件:

class PropertyBlock extends React.Component {
    render() {
        const { headtext, blockType, selectedId, selectOptions, onChange, extratext } = this.props
        return (
            <div className='propblk'>
                <div className='headline'>{headtext}</div>
                {blockType == 'dropdown' ? <PropBlk_Dropdown selectOptions={selectOptions} selectedId={selectedId} onChange={onChange} />
                    :
                    blockType == 'inp_line' ? <PropBlk_InputLine value={selectedId} onChange={onChange} />
                        :
                        blockType == 'inpstory' ? <PropBlk_InputStory value={selectedId} onChange={onChange} extratext={extratext} />
                            :
                            blockType == 'showline' ? <PropBlk_ShowLine value={selectedId} lines={selectOptions} />
                                :
                                blockType == 'inp_date' ? <PropBlk_InputDate value={selectedId} onChange={onChange} />
                                    :
                                    blockType == 'inp__chk' ? <PropBlk_Checkbox value={selectedId} onChange={onChange} extratext={extratext} />
                                        :
                                        <div>
                                            {selectedId}
                                        </div>

                }
            </div>
        )
    }
}

但不知何故,我认为这可能会更好。我在考虑一个parent组件和许多继承自该父组件的专业child组件。喜欢:

class PropBlk_Type1 extends PropertyParentBlock {
   ...
}

但是,阅读On React似乎应该避免继承。所以我在React示例中试了一下这样的东西:

class PropertyParentBlock extends React.Component {
    render() {
        const { headtext, myownhtml } = this.props
        return (
            <div className='propblk'>
                <div className='headline'>{headtext}</div>
                {myownhtml}
            </div>
        )
    }
}

class PropBlk_Type1 extends React.Component {
    render() {
        const { headtext, value, onChange, extratext } = this.props
        return (
            <PropertyParentBlock headtext={headtext}
                myownhtml={(
                    <div>
                        <input value={value} onChange={(e) => onChange(e.target.value)} />
                    </div>
                )}
            />
        )
    }
}

因此,专门的html作为属性myhtml加载到父组件中。使用WebPack构建它没有给出任何语法错误,实际运行它会创建所需的html,它似乎工作。

当我还在学习React时,我想知道我是否在正确的轨道上,或者有更好的方法和最佳实践来进行这种继承或组合。

reactjs inheritance composition
2个回答
1
投票

没有必要像你一样嵌套三元运算符,你可以这样做:

class PropertyBlock extends React.Component {
    render() {
        const { headtext, blockType, selectedId, selectOptions, onChange, extratext } = this.props;
        let element;
        switch(blockType){
           case "dropdown" : 
               element =  <PropBlk_Dropdown selectOptions={selectOptions} selectedId={selectedId} onChange={onChange} />;
                 break;
            // go on here ...
        }
        return (
            <div className='propblk'>
                <div className='headline'>{headtext}</div>
                {element}
            </div>
        )
    }
}

0
投票

通过更仔细地阅读React文档,我发现我走在了正确的轨道上。只留下一对括号并使用特殊的props.children,只能简化我的例子。

我可以将问题的丑陋代码重写为类似“父”组件的东西:

class PropBlk_Frame extends React.Component {
    render() {
        const { headtext } = this.props
        return (
            <div className='propblk'>
                <div className='headline'>{headtext}</div>
                {this.props.children}
            </div>
        )
    }
}

上面的组件用于许多“子”组件,例如:

class PropBlk_InputLine extends React.Component {
    render() {
        const { headtext, value, inputfield, onChange } = this.props
        return (
            <PropBlk_Frame headtext={headtext}>
                <input value={value} onChange={(e) => onChange(true, e.target.value, inputfield)} onBlur={() => onChange(false, null, inputfield)} />
            </PropBlk_Frame>
        )
    }
}

class PropBlk_InputDate extends React.Component {
    render() {
        const { headtext, value, inputfield, onChange } = this.props
        return (
            <PropBlk_Frame headtext={headtext}>
                <input type='date' value={value} onChange={(e) => onChange(true, e.target.value, inputfield)} onBlur={() => onChange(false, null, inputfield)} />
            </PropBlk_Frame>
        )
    }
}

现在我可以用这样的专用组件构建我的网页:

<PropBlk_InputLine headtext='Projectnaam *' value={fields.ProjectNaam} inputfield='inp_projectnaam' onChange={this.onInput} />
<PropBlk_InputDate headtext='Opdrachtdatum *' value={fields.DatumLabOpdracht} inputfield='inp_opdrachtdatum' onChange={this.onInput} />

这种方法消除了三元组的结构,因此节省了代码行,并且更易于理解。

© www.soinside.com 2019 - 2024. All rights reserved.