我使用 i18next 在单独的 repo 中翻译我的组件。现在我需要使用主项目中一个组件的方法。这是示例组件:
class ExampleComponent extends React.Component {
constructor(props) {
this.state = {
text: '',
};
this.resetText = this.resetText.bind(this);
}
resetText() {
this.setState({
text: '',
});
}
render() {
<div/>
}
export default translate('components', { withRef: true })(ExampleComponent);
Run Code Online (Sandbox Code Playgroud)
现在我需要在我的主项目中使用 resetText,没有翻译它工作正常
class MainComponent extends Component {
resetComponent() {
return () => this.exampleComponent.resetText();
}
renderExampleComponent() {
return (
<ExampleComponent
ref={(exampleComponent) => { this.exampleComponent = exampleComponent; }}
/>
);
}
render() {
return (
<div>
{this.resetComponent()}
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
我试过 this.refs.exampleComponent.resetText(); …
我还在学习 js,我需要一个建议。我从后端获取 json 对象,但键有下划线。将键重命名为例如 folder_name 为 Folder name 的最佳做法是什么?属性列表是已知的并已完成,因此我可以将新名称保留在常量中。在前端,我已经像这样使用它:
const showPropertiesList = properties => Object.keys(properties).map(property => (
<PropertyKey key={property}}>
`${property}: ${properties[property]}`
</PropertyKey>
));
Run Code Online (Sandbox Code Playgroud)
最好在此映射中使用重命名函数或在获取所有重命名的键值之前创建单独的函数?
json文件:
properties {
folder_name: 'test',
user_email: 'test@example.com',
user_agreed: 1,
site: 'example.com',
}
Run Code Online (Sandbox Code Playgroud)