27

I am migrating js to ts and have an error with my modified code:

Line 26:8: Parsing error: '>' expected

import React from "react";
import { Route, Redirect, RouteProps } from "react-router-dom";
import {render} from "react-dom"
import {AppProps} from "../App"

function querystring(name: string, url = window.location.href) {
  name = name.replace(/[[]]/g, "\\$&");

  const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)", "i");
  const results = regex.exec(url);

  if (!results) {
    return null;
  }
  if (!results[2]) {
    return "";
  }

  return decodeURIComponent(results[2].replace(/\+/g, " "));
}
export default function UnauthenticatedRoute({ component: C, appProps, ...rest }:
                                                 RouteProps & {appProps: AppProps}) {
  const redirect = querystring("redirect");
  return (
    <Route
        {...rest} // the issue is here
        render={ props => !appProps.isAuthenticated ?
            <C {...props} {...appProps} />
          :
            <Redirect to={redirect === "" || redirect === null ? "/" : redirect}/>
    }
    />
  );
}

If someone sees the problem, that would be kind :). Thanks!


Solution

1/ File needs to have tsx extension

2/ syntax was also wrong in the tsx syntax. I changed to this and now it's ok:

export default function UnauthenticatedRoute({ component: C, appProps, ...rest }:
                                                 RouteProps & {appProps: AppProps}) {
  const redirect = querystring("redirect");
  return (
    <Route {...rest}
        render={ props => !appProps.isAuthenticated ?
            <C {...props} {...appProps} />
          :
            <Redirect to={redirect === "" || redirect === null ? "/" : redirect}/>
        }
    />
  );
}

Now I have another issue with binding element 'C', but it's another story.

Thanks everyone!

6
  • This is a *.tsx formatted file. Probably it's sufficient to change the file extension. Commented Feb 27, 2020 at 17:09
  • @Andreas_D That's what I did. Same error. Commented Feb 27, 2020 at 17:11
  • @KevinB You mean arount {...rest}? Not working. Commented Feb 27, 2020 at 17:16
  • No, i meant the arrow function. then realized it's an entirely different line. but i still think it's in some way relevant to the problem Commented Feb 27, 2020 at 17:17
  • 1
    @Andreas_D yes there was an isssue with the syntax but just by changing spacing and next line, it got fixed. Don't know the exact syntax issue though. Thx anyway. Commented Feb 27, 2020 at 17:56

3 Answers 3

54

Change the file extension from .ts to .tsx.

Sign up to request clarification or add additional context in comments.

1 Comment

ok I changed to tsx and changed a bit the spacing around the tsx returned and it worked.
3

extension must be: .TSX NOT .ts

if the file extension is already .tsx, then you need to Restart your app, cause in Cache memory, the extension is still .ts

just restart or rebuild the app.

if it is: Create React App, then do this in terminal:

  • ctrl + c
  • npm start

should be fixed now

Comments

0

Just take note: when returning react-dom, always use TSX otherwise TS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.