程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了我怎样才能通过短方式控制道具?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决我怎样才能通过短方式控制道具??

开发过程中遇到我怎样才能通过短方式控制道具?的问题如何解决?下面主要结合日常开发的经验,给出你关于我怎样才能通过短方式控制道具?的解决方法建议,希望对你解决我怎样才能通过短方式控制道具?有所启发或帮助;

我想知道我们怎样才能让这个案例变得容易?

我有一个自定义的 Typeahead 组件,这将使我的一些自定义操作。所以我想通过TypeaheadCustom组件的props来控制Typeahead的props:

在组件中我调用 TypeaheadCustom :

<TypeaheadCustom propsControler />

在 TypeaheadCustom 组件中:

function TypeaheadCustom({ propsControler,children,...otherProps },ref) {
    if(propsControler=== true) { //make some propsOfTypeahead}
        return (
            <Fragment>
                <Typeahead ref={ref} {...otherProps} //propsOfTypeahead>
                    {children}
                </Typeahead>
            </Fragment>
        );
    }
}

我想要这样做,因为我想要它干净。我们怎么做?谢谢

解决方法

您可以为此编写一个 HoC。在 HoC 中,您传递了 propsControler。如果为真,则返回组件,否则返回 null。这是伪代码

`

function TypeaheadCustom(propsControler,childrenComponent,ref) {
    return propsControler === true ? (<Fragment>
        <Typeahead ref={ref} {...otherProps}> //propsOfTypeahead>
        {<childrenComponent />} // It shall have the props of its own 
            </Typeahead>
    </Fragment>) : null
}
}

`

,

您可以在条件内创建对象,并将该对象作为道具传递

function TypeaheadCustom({ propsControler,children,...otherProps },ref) {
    if(propsControler=== true) { 
        const propsOfTypeahead = {prop1: 1,prop2: 2}
        return (
            <Fragment>
                <Typeahead ref={ref} {...otherProps} {...propsOfTypeahead}>
                    {children}
                </Typeahead>
            </Fragment>
        );
    }
}

大佬总结

以上是大佬教程为你收集整理的我怎样才能通过短方式控制道具?全部内容,希望文章能够帮你解决我怎样才能通过短方式控制道具?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: