小编Kan*_*hka的帖子

用于非组件的React-intl

当前,我有以下代码将react-intl暴露给非组件,但是它为intl引发了未定义错误。

我创建了一个单独的组件,称为“ CurrentLocale”,并向其注入了int-intl。导出函数t将使用CurrentLocale上下文中的intl formatMessage。

import React from 'react';
import {injectIntl} from 'react-intl';
import PropTypes from 'prop-types';
import { flow } from 'lodash';

class CurrentLocale extends React.Component {


    constructor(props,context){

        super();
        console.log(context,props);
        console.log(this.formatMessage);
        const { intl } = this.context.intl;//this.props;

        this.formatMessage = intl.formatMessage;
    }

    render() {
        return false;
    }
}
CurrentLocale.contextTypes={
    intl:PropTypes.object,
};


injectIntl(CurrentLocale);

function intl() {
    return new CurrentLocale();
}

function formatMessage(...args) {
    return intl().formatMessage(...args);
}


const t = opts => {
    const id = opts.id;
    const type = opts.type;
    const values …
Run Code Online (Sandbox Code Playgroud)

reactjs react-intl

8
推荐指数
3
解决办法
7614
查看次数

标签 统计

react-intl ×1

reactjs ×1