【发布时间】:2019-06-09 21:10:40
【问题描述】:
我在 nextJS 应用程序中使用一些 HOC 组件通过getInitialProps 设置一些道具值。
但我需要为这些道具使用动态值。
在我的索引组件中,我调用了withServerProps。是否可以将一些字符串数组传递给它?
index.js
import React, { Component } from 'react'
import { withTranslation } from 'i18n'
import withServerProps from 'with-server-props'
class Index extends Component {
render () {
return (<div>Some content</div>)
}
}
export default withServerProps( // <-- How to pass an array with strings?
withTranslation()(Index)
)
我需要在这个函数中获取字符串数组:
with-server-props.js
import React, { Component } from 'react'
export default WrappedComponent =>
class extends Component {
static async getInitialProps (context) {
const { query } = context
return {
id: query && query.id
target: PASSED_ARRAY // <-- Need to recieve the array here
}
}
render () {
return <WrappedComponent {...this.props} />
}
}
【问题讨论】:
标签: javascript reactjs high-order-component