3

Could you please help me with this?

What I want to do is check if the user is registered. If it is registered it should show the home url and if it is not registered it should show the registration url.

For that I see and check the saved data (isRegistered), if it equals to null is not registered and should show the /register url. But its not working.

In the next line I have an error

const [isReg, setIsReg] = useState(false);

And it gives me this error:

Expression expected.ts(1109)

const App: React.FC = () => (
  
  const [isReg, setIsReg] = useState<boolean>(false);
  
  if(getItem("isRegistered")==null){
    console.log(getItem("isRegistered"));
    useState(true);
  }
  else{
    useState(false);
  }

  <IonApp>
    <IonReactRouter>
    <IonSplitPane contentId="main" when="(min-width: 4096px)">
          <Menu />
      <IonRouterOutlet id="main">

        <Route path="/" render={() => isReg ? {Home} : {Registro}} />
        
        <Route path="/Favorites" component={Favoritos} exact={true}></Route>
     

      </IonRouterOutlet>
      </IonSplitPane>
    </IonReactRouter>
  </IonApp>
);

enter image description here

1
  • You need to use setIsReg(true) or false depending the logic and wrap the condition inside a React useEffect hook. Commented Jan 14, 2021 at 22:28

1 Answer 1

1

it should be

useEffect(() => {
    if(getItem("isRegistered")==null){
    console.log(getItem("isRegistered"));
    setIsReg(true);
  }
  else{
    setIsReg(false);
  }
}, [])

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

6 Comments

thanks but still at const I get: SyntaxError: /home/julian/Documentos/ServicesApp/src/App.tsx: Unexpected token (38:2)
which const do you mean? What line in the code exactly?
the second line: const [isReg, setIsReg] = useState(false);
can you remove the <boolean> part and see if it works? You don't need to define the type, if your initial state is from that type any way.
I know why, you app function is not wrapped in curly braces {}. It is wrapped inside normal brackets (); And wrap the jsx part inside of a return statement.
|

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.