Enhance component with preset properties.
npm install @jswork/with-props
import withProps from '@jswork/with-props';
import React from 'react';
interface MyComponentProps {
title: string;
content: string;
}
const MyComponent: React.FC<MyComponentProps> = (props) => {
const { title, content } = props;
return React.createElement('div', null,
React.createElement('h1', null, title),
React.createElement('p', null, content)
);
};
// 使用 withProps 直接传递 defaultProps 和组件
const EnhancedComponent = withProps({ title: 'Default Title' }, MyComponent);
const App: React.FC = () => {
return React.createElement('div', null,
React.createElement(EnhancedComponent, { content: 'Custom Content' })
);
};
export default App;
Code released under the MIT license.