как я могу добавить запрос в react router

#reactjs #routes #react-router

#reactjs #маршруты #react-маршрутизатор

Вопрос:

теперь это

http://localhost:8001/components/button/

Я хочу

http://localhost:8001/components/button/?theme=dark

Я использовал

 browserHistory.push({
  pathname: '',
  query: {theme: dark},
})

and

browserHistory.push({
  pathname: 'components/button',
  query: {theme: dark},
})

and 

browserHistory.push({
  pathname: 'button',
  query: {theme: dark},
})
  

Все завершилось неудачей.

Как я могу сделать? Можете ли вы мне помочь, спасибо!

Комментарии:

1. вы не можете просто сделать browserHistory.push("/components/button/?theme=dark") или попробовать browserHistory.push({ pathname: 'components/button', search: '?theme: dark', })

2. результатом @dikuw является localhost:8001/components /button/компоненты / кнопка /?theme = темный

Ответ №1:

Вы могли бы просто использовать string literal и нажать на историю.

 browserHistory.push(`components/button?theme=${dark}`)
  

Комментарии:

1. Я использовал. это то же самое, что ` pathname: ‘components / button’,` и результатом является localhost: 8001/components/button/компоненты / кнопка /?theme= dark

Ответ №2:

Вы можете использовать push метод как:

 browserHistory.push({
  pathname: '/components/button',
  search: '?theme=dark'
})
  

или просто как:

 browserHistory.push('/components/button?theme=dark')
  

Комментарии:

1. Это будет /components /button/компоненты /кнопка/?theme= темный

2. это /components /button / , а не /components /button . Так что это не работает.