Typescript - Left side of comma operator is unused and has no side effects - How to use constants in place of strings in hookrouter's route?
Asked Answered
S

2

6
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;

May I know how to use constants instead of strings in the routes as follows please?

const root = "/";

const routes = {
  `${root}`: () => <Users />,
};

When I tried the code above, I got the following error:

Left side of comma operator is unused and has no side effects
Seem answered 13/6, 2020 at 7:18 Comment(4)
What if you put parentheses around the JSX?Triangular
@Triangular Could you let me know where exactly please?Seem
Exactly "around the JSX", () => (<Users />),.Triangular
@Triangular ${root}`: () => (<Users />), Still I get the same errorSeem
C
4

The syntax for a computed property name in an object literal is [someExpression], not `${someExpression}`:

const root = "/";

const routes = {
  [root]: () => <Users />,
};

[Playground Link]

Chane answered 13/6, 2020 at 8:22 Comment(0)
L
1

Got the issue when I was using an arrow function by returning a key value pair object , below is the code when issue occurred

const methodTest = (value) => { key1 : value,key2:value };

The solution is to add a () around the braces {} . Below is the solution for the above issue

const methodTest = (value) => ({key1:value , key2:value });

The fix is to wrap the object literal in parentheses

Landon answered 22/5, 2023 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.