0
HOCラッパーを使用してコンポーネントルートをレンダリングしようとすると無限ループに遭遇します。時間ビーイングのためにルーティングで適用するとHOC無限ループに反応します
const render = (route, additionalProps) => {
const scrollActive = has(route, "scroll") ? route.scroll : true;
const Component = scrollActive
? withScrollToTop(route.component)
: route.component;
return props => {
return <Component {...props} {...additionalProps} route={route} />;
};
return (
<Switch>
{routes.map((route, i) => {
return (
<Route
key={i}
path={route.path}
exact={route.exact}
strict={route.strict}
render={render(route, injectProps)}
/>
);
})}
</Switch>
);
(それが出てループだので)、HOCが実際に別のクラスとコンポーネントを包む以外に何もしていません:ルーティングは、メソッドのようなものに見えるレンダリングします。
export default function withScrollToTop(WrappedComponent) {
const displayName = `HigherOrder.WithScrollToTop('${getDisplayName(
WrappedComponent
)}')`;
class WithScrollToTop extends React.PureComponent {
static displayName = displayName;
static WrappedComponent = WrappedComponent;
render() {
const props = Object.assign({}, this.props);
return React.createElement(WrappedComponent, props);
}
}
return hoistStatics(WithScrollToTop, WrappedComponent);
}
任意のルートをヒットしようとすると、無限ループが発生します。
申し訳ありませんが、それはレンダリング小道具の三角形を削除したことによる誤植です。私たちは工場でレンダリングする別の機能を持っています。不足しているブレースを修正するために、元のスニペットを更新しました。 – mindlis
はい、正しいタイプのイベントです。代わりに 'render = {this.render ...}'または 'render = {()=> this.render ...}'を呼び出すべきです。バインディングの方法は 'render'関数を' props'に 'render'を渡さずにすぐ実行します –