import React from "react";
import Users from "./components/Users";
import Contact from "./components/Contact";
import About from "./components/About";
const routes = {
"/": () => <Users />,
"/about": () => <About />,
"/contact": () => <Contact />
};
export default routes;
我可以知道如何在路由中使用常量而不是字符串,如下所示吗?
const root = "/";
const routes = {
`${root}`: () => <Users />,
};
当我尝试上面的代码时,我收到以下错误:
Left side of comma operator is unused and has no side effects
对象文本中计算属性名称的语法是[someExpression]
,而不是`${someExpression}`
:
const root = "/";
const routes = {
[root]: () => <Users />,
};
[游乐场链接]
当我通过返回键值对对象使用箭头函数时遇到问题,下面是出现问题时的代码
const methodTest = (value) => { key1 : value,key2:value };
解决方案是在大括号{}
周围添加一个()
。以下是上述问题的解决方案
const methodTest = (value) => ({key1:value , key2:value });
解决方法是将对象文本括在括号中