简体   繁体   English

如何从父组件获取 intl.formatMessage?

[英]How can get intl.formatMessage from parent component?

How can I get intl.formatMessage from parent component?如何从父组件获取 intl.formatMessage? I wrapped parent component with injectIntl and want send intl.formatMessage to child component.我用 injectIntl​​ 包裹了父组件,并希望将 intl.formatMessage 发送到子组件。 Can someone help me with that?有人可以帮我吗? Thank you!谢谢!

Parent component父组件

import Car from "./test3";
import { injectIntl } from "react-intl";

class Intl extends React.Component {
  render() {
    return (
      <div>
        <h1>Who lives in my garage?</h1>
        <Car brand="Ford" />
      </div>
    );
  }
}

export default injectIntl(Intl);

Child component子组件

import { FormattedMessage} from "react-intl";

class Car extends React.Component {
  yearsTranslation = () =>
    this.props.intl.formatMessage({ id: "search.filter.maturity.years" });
  render() {
    return <h2>Hello {this.yearsTranslation()}!</h2>;
  }
}

export default Car;

Just pass the prop down, like so :只需将道具传递下去,就像这样:


class Intl extends React.Component {
  render() {
    return (
      <div>
        <h1>Who lives in my garage?</h1>
        <Car brand="Ford" intl={this.props.intl}/>
      </div>
    );
  }
}

export default injectIntl(Intl);

I thinks intl is available in the context.我认为intl在上下文中可用。 Please check the documentation.请检查文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何向 intl.formatMessage 添加数据? - How to add data to intl.formatMessage? 在玩笑测试时如何在react component方法中将react-intl作为参数传递,抛出&#39;TypeError:intl.formatMessage不是一个函数&#39; - how to pass react-intl as argument in react component method while jest testing, throws 'TypeError: intl.formatMessage is not a function' 如何使用 react-intl 使用标签(链接)格式化消息? - How can I formatMessage with a tags (links) using react-intl? react-intl将react组件作为formatMessage中的占位符 - react-intl render react component as placeholder in formatMessage 如何使用React-Intl使用injectIntl​​ formatMessage插入HTML标记? - How to insert HTML tag with injectIntl formatMessage using React-Intl? 我可以从子组件到父组件获取计算数据吗? - Can I get computed data from child component to parent component? 无法从子组件获取在父组件上执行的方法 - Can't get method to execute on parent component from child component 如何在反应中从子组件获取数据到父组件? - how to get data from child component to parent component in react? 如何在React Native中从子组件回调到父组件 - How to get callback from child component to parent component in React Native 如何从父组件访问嵌套组件? - How can I accessing nested component in react from parent component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM