如何从父组件获得intl.formatMessage?我用injectIntl包装父组件,并希望将intl.formatMessage发送到子组件。有人能帮我吗?谢谢!
父组件
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);子组件
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;发布于 2019-12-13 11:36:09
把道具递下来,就像这样:
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);发布于 2019-12-13 11:39:15
我认为intl在上下文中是可用的。请查一下文件。
https://stackoverflow.com/questions/59321733
复制相似问题