[英]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.