Javascript 如何使用道具?

Javascript 如何使用道具?,javascript,reactjs,react-router,react-redux,Javascript,Reactjs,React Router,React Redux,我有个问题) 我有路由器 <Switch> <Route exact path={`/:lng(en|ru)?`} component={HomeView} /> ...... <Route component={NotFoundView} /> </Switch> 我有很多组件,每个组件都必须包装成I18n组件。这并不方便。我是否可以将所有组件包装一次到I18n,并在I18n grab{this.props.match.paraments}

我有个问题)

我有路由器

<Switch>
<Route exact path={`/:lng(en|ru)?`} component={HomeView} />

......

<Route component={NotFoundView} />
</Switch>
我有很多组件,每个组件都必须包装成I18n组件。这并不方便。我是否可以将所有组件包装一次到I18n,并在I18n grab
{this.props.match.paraments}

  • 您可以使用HOC创建函数 例如:
  • 具有i18n(Comp,lang)的函数{
    返回类I18nWrapper扩展React.Component{
    ...
    render(){
    返回(
    );
    }
    }
    }
    //在路由器中,您可以使用withI18n Hoc和路由组件的渲染属性
    {
    返回i18n(HomeView,match.path)
    }}
    ......
    
    也许可以围绕使用路由器的
    构建您自己的包装器
    ?让我知道我是否应该详细说明这个想法:)在I18n组件中已经与路由器一起使用,但是如果例如location=/en将语言设置分别传递给每个I18n HOC没有意义,则
    {this.props.match.parameters}
    为空。您应该在提供者层上设置一次。但是,如果我将所有组件在顶层封装一次到I18n中,我可以在I18n中使用
    t
    {this.props.match.paraments}`例如
    location.pathname.split('/')[1]
    我不明白I18n是什么意思<代码>函数mapStateToProps(state){return{…state.i18n}}函数mapDispatchToProps(dispatch){return{actions:bindActionCreators({…change},dispatch),dispatch}类i18n扩展React.Component{构造函数(props){super(props);}changeLang{this.props.actions.change(lang)}render(){return({this.props.children});}}使用路由器导出默认值(connect(MapStateTops,mapDispatchToProps)(I18n));这是我的I18n组件
    return (
    
        <I18n lang={this.props.match.paraments}>
                    .....
        </I18n>
    
    )