gpt4 book ai didi

javascript - React spread operator (...rest) 替代方案?

转载 作者:行者123 更新时间:2023-11-29 11:00:02 25 4
gpt4 key购买 nike

我无法在我的管道中包含 babel-preset-stage-3。除了传播运算符之外,还有其他选择吗?

我正在尝试编译以下代码,但出现语法错误:

const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props =>
fakeAuth.isAuthenticated ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
)
}
/>
);

最佳答案

使用 lodash.omit :

const PrivateRoute = (props) => {
const Component = props.component;
const rest = omit(props, ['component'])
return (
<Route
{...rest}
render={props =>
fakeAuth.isAuthenticated ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
)
}
/>
);
}

关于javascript - React spread operator (...rest) 替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48893556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com