Можно ли передать массив строк в качестве параметра, используя HOC? - PullRequest
0 голосов
/ 10 июня 2019

Я использую некоторые компоненты HOC в своем приложении nextJS, чтобы установить некоторые значения пропуска при помощи 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) 
)

Мне нужно получить массив строк в этой функции:

с-сервер 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} />
    }
  }

1 Ответ

1 голос
/ 10 июня 2019

Да, вы определенно можете.Просто добавьте несколько аргументов во время экспорта в index.js.

export default withServerProps(withTranslation()(Index), ["hello"])

Затем в вашем HOC:

export default function handleServerProps(WrappedComponent, arr) {
  class Hoc extends Component {
    static async getInitialProps (context) {
      const { query } = context
      return {
        id: query && query.id,
        target: arr,
      }
    }
    render () {
      return <WrappedComponent {...this.props} />
    }
  }

  return Hoc;
}
...