Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在React中使用继承反转模式有哪些缺点_Reactjs_Higher Order Components - Fatal编程技术网

Reactjs 在React中使用继承反转模式有哪些缺点

Reactjs 在React中使用继承反转模式有哪些缺点,reactjs,higher-order-components,Reactjs,Higher Order Components,我特别感兴趣的是在React中进行条件渲染时使用继承反转,这要归功于父组件通过this的可访问性 假设我有一个带有标签的HOC,它接受一个输入组件,并在其外部包装一个标签(如果提供了标签)。所以API看起来像这样: const TextInputWithLabel = withLabel(TextInput) <TextInputWithLabel label="foo" id="bar"/> <TextInputWithLabel /> const TextInput

我特别感兴趣的是在React中进行条件渲染时使用继承反转,这要归功于父组件通过
this
的可访问性

假设我有一个带有标签的
HOC,它接受一个
输入
组件,并在其外部包装一个标签(如果提供了标签)。所以API看起来像这样:

const TextInputWithLabel = withLabel(TextInput)
<TextInputWithLabel label="foo" id="bar"/>
<TextInputWithLabel />
const TextInputWithLabel=withLabel(TextInput)
分别返回

 <label htmlFor="bar">
     foo
     <input type="text" id="bar"/>
 </label>


虽然我听说有些人不喜欢它,但他们不能给我令人信服的理由


您有任何理由反对在中/大型代码库中使用II吗?例如,我正在寻找不需要使用II的地方,或者解释为什么我不应该在这种特殊情况下使用II。

我唯一的问题是,您使用的ID会影响组件重用。此外,对于终端用户来说,一个公平的假设是,他们提供了一个标签,因此您不需要覆盖无标签的情况(可能根据您的情况使用PropTypes或TS),以保持预期的行为。使用ID实际上是使标签起作用的原因。你可以在@ctn上看到更多,然后允许将其作为道具传入。是的,我确实这样做了。很抱歉造成混淆,请编辑问题rn。
<input type="text"/>