У меня есть функциональный компонент, который требует рендеринга реквизитов height
и width
. Давайте назовем это PureSizableDiv
:
const PureSizableDiv = ({ height, width }) =>
<div style={{ height, width }}>I'm a div!</div>
У меня также есть контекст React, называемый Size
:
import React from 'react';
import { compose, fromRenderProps } from 'recompose';
export const { SizeProvider, SizeConsumer } = React.createContext({
height: null,
width: null,
});
Вместо того, чтобы вручную создавать новый HoC, подобный этому:
export const withSize = Component => (props) =>
<SizeConsumer>
{
({ height, width }) =>
<Component
height={height}
width={width}
{...props}
/>
}
</SizeConsumer>;
Я хотел бы знать, есть ли более короткий более чистый способ использования recompose
для этого.
Я пытался
export const withSize = compose(
fromRenderProps(SizeConsumer, ({ height, width }) => ({ height, width })),
);
Но получил ошибку Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.