简体   繁体   English

为什么当我尝试在我的功能组件中使用来自道具 object 的匹配参数时它会失败?

[英]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.

相关问题 为什么要使用 Object.assign() 来更新功能组件 props 的变化? - Why to use Object.assign() to update functional component props change? 如何在 React 功能组件中使用 Props 添加 Redux object 作为参数 - How to Add Redux object as a Parameter with Props in React Functional Component 当我构造属性时,为什么我的组件接收道具不起作用,但是当我使用props.key时它正在工作? - Why is my component that receives props not working when I destructure out the properties, but when I use props.key it's working? 为什么我的道具在我需要的时候不能在我的组件中使用? - Why aren't my props available to use in my component when I need them? 为什么我不能在事件处理程序中访问我的功能组件的道具? - Why can't I access my functional component's props within an event handler? 当我尝试在组件上使用数组方法时,为什么我的数组变量未在我的组件中定义? - Why is my array variable undefined in my component when I try to use array methods on it? 为什么我不能在我的 CountUp 组件中使用 `props.data`? - Why cant i use `props.data` in my CountUp component? 当我不将道具传递给功能组件时,为什么打字稿不会抛出错误? - Why isn't typescript throwing an error when I do not pass down props to a functional component? Reactjs 功能组件默认 props 和 props 对象 - Reactjs functional component default props with props object 从功能组件传递道具? - passing props from functional component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM