我想测试我的组件是否可以获取位置的搜索部分并转换为json以供使用,但是当我使用querystring设置initialEntries
时,搜索部分未更改。
//My Test
render(
<MemoryRouter inititalEntries={['/pesquisa?number=1&uf=PR']}>
<App/>
</MemoryRouter>
)
//My Component
const Pesquisa = (props) => {
const { search } = props.location
// what i want
// search === '?number=1&uf=PR'
// what i have
// search === ''
}
export default withRouter(Pesquisa)
// My Container App
const App = () =>
<div className="App">
<BrowserRouter>
<Switch>
<Route path="/pesquisa" render={props => <Pesquisa/>}/>
</Switch>
</BrowserRouter>
</div>
最佳答案
查询参数来自withRouter的匹配项
const { pesquisa } = props.match.params;