有時候我的組件具有大量的屬性。反應:反應組件上的道具數量是否有可觀的限制
這有什麼固有的問題嗎?
例如
render() {
const { create, update, categories, locations, sectors, workTypes, organisation } = this.props; // eslint-disable-line no-shadow
return (
<div className="job-container">
<JobForm
organisationId={organisation.id}
userId={user.id}
action={action}
create={create}
update={update}
categories={categories}
locations={locations}
sectors={sectors}
workTypes={workTypes}
/>
</div>
);
}
什麼是最佳實踐?
我不認爲React組件可以具有的屬性數量有任何問題。它看起來不錯。 – vijayst
你在尋找什麼類型的最佳做法?例如,你可以通過使用spread運算符來縮短你的語法。 – larrydahooster
最好的做法是在組件中使用propTypes來宣告道具的形式。除此之外,您可以隨意放置儘可能多的道具。另外,至少對於公共組件,有很好的默認設置來促進簡潔的使用。 – hazardous