[英]Why it fails when I try to use match parameter from props object in my functional component?
It works fine this way when I use props.match.url
.当我使用
props.match.url
时,它可以正常工作。
import {withTranslation} from 'react-i18next';
function Navbar ({props}) {
return (
<nav className={css.navbar}>
<NavLink exact to={`${props.match.url}`} className={css.link} activeClassName={css.linkActive}>
Calls receive scenario
</NavLink>
<NavLink to={`${props.match.url}/call-tracking`} className={css.link} activeClassName={css.linkActive}>
Tracked numbers
</NavLink>
<NavLink to={`${props.match.url}/greetings`} className={css.link} activeClassName={css.linkActive}>
Greetings
</NavLink>
<NavLink to={`${props.match.url}/sip`} className={css.link} activeClassName={css.linkActive}>
Sip accounts
</NavLink>
</nav>);
}
export default withTranslation()(Navbar);
But when I add Flow type checking (I'm almost entirely sure I'm doing it right), it logs the error.但是当我添加流类型检查(我几乎完全确定我做对了)时,它会记录错误。
import {withTranslation} from 'react-i18next';
type Props = {
t: Function,
match: {
url: string
}
}
function Navbar({t, match}: Props) {
return (
<nav className={css.navbar}>
<NavLink exact to={`${match.url}`} className={css.link} activeClassName={css.linkActive}>
{t('settings.page_vats.page_scenario.navbar_title')}
</NavLink>
<NavLink to={`${match.url}/call-tracking`} className={css.link} activeClassName={css.linkActive}>
Tracked numbers
</NavLink>
<NavLink to={`${match.url}/greetings`} className={css.link} activeClassName={css.linkActive}>
Greetings
</NavLink>
<NavLink to={`${match.url}/sip`} className={css.link} activeClassName={css.linkActive}>
Sip accounts
</NavLink>
</nav>);
}
export default withTranslation()(Navbar);
The error looks this way:错误看起来是这样的:
Uncaught RuntimeTypeError: Props.match must be an object
Expected: {
url: string;
}
Actual Value: undefined
Actual Type: void
Apparently, I passed all props from the parent component.显然,我从父组件传递了所有道具。
function ParentComponent (props) {
return (
<>
<Navbar props={props} />
<Routes props={props} />
</>
);
}
I guess this is not a good practice to name the props in the same way.我想以同样的方式命名道具并不是一个好习惯。
So I made a little change and it got easier to figure out how to receive necessary properties from incoming props object.所以我做了一点改变,更容易弄清楚如何从传入的道具 object 接收必要的属性。
function ParentComponent (props) {
return (
<>
<Navbar parentComponent={props} />
<Routes parentComponent={props} />
</>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.