component-hook-factories

Nested components அல்லது hooks-ஐ வரையறுக்கும் higher order functions-ஐ எதிர்த்து validate செய்கிறது. Components மற்றும் hooks module level-இல் வரையறுக்கப்பட வேண்டும்.

விதி விவரங்கள்

மற்ற functions உள்ளே components அல்லது hooks-ஐ வரையறுப்பது ஒவ்வொரு call-இலும் புதிய instances உருவாக்கும். React ஒவ்வொன்றையும் முற்றிலும் வேறு component ஆகக் கருதி, முழு component tree-ஐ அழித்து மறுபடியும் உருவாக்கும்; இதனால் state அனைத்தும் இழக்கப்படும் மற்றும் performance பிரச்சினைகள் ஏற்படும்.

செல்லாதது

இந்த விதிக்கான தவறான code உதாரணங்கள்:

// ❌ Factory function creating components
function createComponent(defaultValue) {
return function Component() {
// ...
};
}

// ❌ Component defined inside component
function Parent() {
function Child() {
// ...
}

return <Child />;
}

// ❌ Hook factory function
function createCustomHook(endpoint) {
return function useData() {
// ...
};
}

செல்லுபடியாகும்

இந்த விதிக்கான சரியான code உதாரணங்கள்:

// ✅ Component defined at module level
function Component({ defaultValue }) {
// ...
}

// ✅ Custom hook at module level
function useData(endpoint) {
// ...
}

Troubleshooting

Dynamic component behavior தேவை

Customized components உருவாக்க factory தேவை என்று நினைக்கலாம்:

// ❌ Wrong: Factory pattern
function makeButton(color) {
return function Button({children}) {
return (
<button style={{backgroundColor: color}}>
{children}
</button>
);
};
}

const RedButton = makeButton('red');
const BlueButton = makeButton('blue');

அதற்கு பதிலாக JSX-ஐ children ஆக pass செய்யுங்கள்:

// ✅ Better: Pass JSX as children
function Button({color, children}) {
return (
<button style={{backgroundColor: color}}>
{children}
</button>
);
}

function App() {
return (
<>
<Button color="red">Red</Button>
<Button color="blue">Blue</Button>
</>
);
}