【发布时间】:2018-09-28 04:36:10
【问题描述】:
我正在编写一个模块,如果用户在他的项目react-intl 中安装了该模块,我希望它能够导出具有翻译功能的高级组件。
这样我就不必维护我的组件的两个版本以避免安装警告。
我一直在尝试使用optionalDepenceny,但是当他安装我的包时,它们已安装在用户的项目中。
通常,这是我要导入的源
/**
*
* ToggleOption
*
*/
import React from 'react';
import PropTypes from 'prop-types';
import Option from 'bootstrap-styled/lib/Option';
let injectIntl;
let intlShape;
// this make react-intl optional to our component and our module
try {
const reactIntl = require('react-intl'); // eslint-disable-line
injectIntl = reactIntl.injectIntl; // eslint-disable-line
intlShape = reactIntl.intlShape; // eslint-disable-line
} catch (er) {
injectIntl = null;
intlShape = null;
}
/**
* This component is automatically used when using `<Toggle />`
* If you need a different option tag, instead just pass the prop `optionTag` of `<Toggle />`.
*/
const ToggleOption = ({
tag: Tag,
value,
message,
intl,
}) => (
<Tag value={value}>
{message && intl ? intl.formatMessage(message) : value}
</Tag>
);
ToggleOption.defaultProps = {
tag: Option,
};
/* eslint-disable react/require-default-props */
ToggleOption.propTypes = {
/**
* Replace the default component tag by the one specified. Can be:
*/
tag: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
PropTypes.func,
]),
/**
* The value
*/
value: PropTypes.string.isRequired,
/**
* react-intl messages (optional)
*/
message: PropTypes.object,
};
let exported = ToggleOption; // eslint-disable-line import/no-mutable-exports
if (intlShape) {
/** @ignore */
ToggleOption.propTypes.intl = intlShape.isRequired;
exported = injectIntl(ToggleOption);
}
export default exported;
有没有办法配置我的模块来做到这一点?
【问题讨论】:
标签: javascript node.js reactjs npm ecmascript-6