<commit_before>import React from 'react'
import { connect, Provider } from 'react-redux'

const __NEXT_REMATCH_STORE__ = '__NEXT_REMATCH_STORE__'

// https://github.com/iliakan/detect-node
const checkServer = () =>
  Object.prototype.toString.call(global.process) === '[object process]'

const getOrCreateStore = (initStore, initialState) => {
  // Always make a new store if server
  if (checkServer() || typeof window === 'undefined') {
    return initStore(initialState)
  }

  // Memoize store in global variable if client
  if (!window[__NEXT_REMATCH_STORE__]) {
    window[__NEXT_REMATCH_STORE__] = initStore(initialState)
  }
  return window[__NEXT_REMATCH_STORE__]
}

export default (...args) => Component => {
  // First argument is initStore, the rest are redux connect arguments and get passed
  const [initStore, ...connectArgs] = args

  const ComponentWithRematch = (props = {}) => {
    const { store, initialProps, initialState } = props

    // Connect page to redux with connect arguments
    const ConnectedComponent = connect.apply(null, connectArgs)(Component)

    // Wrap with redux Provider with store
    // Create connected page with initialProps
    return React.createElement(
      Provider,
      {
        store:
          store && store.dispatch
            ? store
            : getOrCreateStore(initStore, initialState)
      },
      React.createElement(ConnectedComponent, initialProps)
    )
  }

  ComponentWithRematch.getInitialProps = async (props = {}) => {
    const isServer = checkServer()
    const store = getOrCreateStore(initStore)<commit_msg>Fix the bug with creating duplicate components of with-rematch example (#5440)<commit_after>import React from 'react'
import { connect, Provider } from 'react-redux'

const __NEXT_REMATCH_STORE__ = '__NEXT_REMATCH_STORE__'

// https://github.com/iliakan/detect-node
const checkServer = () =>
  Object.prototype.toString.call(global.process) === '[object process]'

const getOrCreateStore = (initStore, initialState) => {
  // Always make a new store if server
  if (checkServer() || typeof window === 'undefined') {
    return initStore(initialState)
  }

  // Memoize store in global variable if client
  if (!window[__NEXT_REMATCH_STORE__]) {
    window[__NEXT_REMATCH_STORE__] = initStore(initialState)
  }
  return window[__NEXT_REMATCH_STORE__]
}

export default (...args) => Component => {
  // First argument is initStore, the rest are redux connect arguments and get passed
  const [initStore, ...connectArgs] = args
  // Connect page to redux with connect arguments
  const ConnectedComponent = connect.apply(null, connectArgs)(Component)

  const ComponentWithRematch = (props = {}) => {
    const { store, initialProps, initialState } = props

    // Wrap with redux Provider with store
    // Create connected page with initialProps
    return React.createElement(
      Provider,
      {
        store:
          store && store.dispatch
            ? store
            : getOrCreateStore(initStore, initialState)
      },
      React.createElement(ConnectedComponent, initialProps)
    )
  }

  ComponentWithRematch.getInitialProps = async (props = {}) => {
    const isServer = checkServer()
    const store = getOrCreateStore(initStore)
{'fraction_non_alphanumeric': 0.10363972856261566, 'fraction_numerical': 0.0012338062924120913, 'mean_word_length': 3.561181434599156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6809157', 'n_tokens_mistral': 935, 'n_tokens_neox': 906, 'n_words': 249}