由于假设 HOC 被用作 mixin,可能与其他 HOC 一起使用,是否最好注入具有上下文但冗长的 prop 名称(例如,以 HOC 的名称为前缀)?
我想这应该被认为是一个最佳实践的问题。
因此,例如,对于这个 HOC 注入类似窗口的行为,哪个更好:
简短的非上下文命名
@window
@otherMixin
class BasicView extends React.Component {
render() {
const {close, open} = this.props; //these are provided by @window
}
}
VS上下文命名(前缀)
@window
@otherMixin
class BasicView extends React.Component {
render() {
const {windowClose, windowOpen} = this.props //these are provided by @window
}
}
或上下文命名(嵌套)
@window
@otherMixin
class BasicView extends React.Component {
render() {
const {window} = this.props; //provided by @window
window.open();
window.close();
/* optionally we can destructure and work with the shorter
* non-contextually named props while still maintaining
* a declaration of their context to disambiguate
*/
//const {window: {open, close} } = this.props;
}
}