用useparams 和props.match.params获取url参数有什么区别
来源:10-3 【详情页面搭建 1】页面框架、详情与日期选择

慕函数4298367
2021-02-19
用useparams 和props.match.params获取url参数有什么区别
写回答
1回答
-
useparams 的原理是 react hook,可以直接在函数式组件中使用,比较方便。
而props.match.params则需要使用withrouter高阶函数包装一下组件,否则无法调用props.match.params
useparams 例子
export const Example = () => { const params = useparams() return <div></div> }
withrouter 例子:
const Example = (props) => { const params = props.match.params // 如果没有使用withrouter,这里会为空或报错 return <div></div> } export const Component = withrouter(Example)
一般来说,使用hooks写出的代码逻辑性会更强、可读性也会更高,所以请尽量使用hooks
012021-03-03
相似问题