I can't implement Redirect in React

nas - i can
i can't
i can only imagine
i can song
i can rap
i can't breathe
i can nas lyrics
nas i can meaning

I want to redirect to the home page when some condition returns null or false but the action of Redirect is not triggered.

import { Link, Redirect } from "react-router-dom";

if(localStorage.getItem("example") === null || localStorage.getItem("example") === false){
    return <Redirect to="/" />
}

I put this code inside in a simple function triggered in one OnClick and componentDidMount(), but it's not working.

You could use Redirect to home page, based on redirect flag that could be changed by using setState in onClickHandler or handleSubmit.

import { Redirect } from "react-router-dom";

class MyComponent extends React.Component {
  state = {
    redirect: false
  }

  handleSubmit () {
    if(localStorage.getItem("example") === null || localStorage.getItem("example") === false){
      return this.setState({ redirect: true });
    }
  }

  render () {
    const { redirect } = this.state;

    if (redirect) {
      return <Redirect to='/'/>;
    }
    return <YourForm/>;
}

Chronixx - "I Can" (Official Music Video), Chronixx - "I Can" taken from the Chronology album out now http://smarturl.it/ ChronologyWW Duration: 4:06 Posted: Feb 26, 2018 The I CAN Enquiry Service continues to operate on Mondays to Wednesdays between 9.30 - 12.30 and 1.30 - 4.30. Please leave voicemail if you cannot get through and we will endeavour to call you back during opening hours. To have a chat with an I CAN speech and language therapist, call 020 7843 2544

You need to use the Redirect inside render. It is a React Component which renders and then sends the user to the desired path:

import React, { Component } from "react";
import { Route, Switch, Redirect } from "react-router-dom";

class RootPage extends React.Component {
  state = {
    isLoggedOut: false
  };

  onClick = () => {
    this.setState({
      isLoggedOut: true
    });
  };

  render() {
    return (
      <div>
        {this.state.isLoggedOut && <Redirect to="/logout" />}
        <button onClick={this.onClick}>Logout</button>
      </div>
    );
  }
}

const Log = () => <h1>Logout</h1>;

class App extends Component {
  render() {
    return (
      <div>
        <nav className="navbar navbar" />
        <Switch>
          <Route exact path="/" component={RootPage} />
          <Route exact path="/logout" component={Log} />
        </Switch>
      </div>
    );
  }
}
export default App;

When you click on the logout button it will redirect you to the rootPath.

Here is the Demo: https://codesandbox.io/s/q9v2nrjnx4

I Can (Nas song), I Know I Can believes that every Columbus City Schools student deserves the opportunity to achieve their dream of earning a college education. And we're here� It has been about 3 and ½ years since Noah received his diagnosis. He is now 7 years old, and is in 1st grade at the local public school. He had initially received services through the public school system, but since earlier this year, he did not meet the criteria for those services.

Have a look at this example in the official docs.

<Redirect /> should be inside your render method if you use a class component. or if you use a function component it should be in what's returned by it.

Example bellow:

import { Component } from 'react';

const PrivateComponent = (props) => {
  return(
    localStorage.getItem("example")
      ? <RandomComponent />
      : <Redirect to="/signin" />
  )
}

Informed Consent Action Network: Home, Plan for college & find financial aid with Iowa College Access Network. Find money for college & get financial aid! By pressing “get started’ I expressly consent to be contacted by iCan/HCCUA, via telephone calls, pre-recorded messages, text messages, and faxes, at the phone numbers you entered into the webpage or any additional numbers you provide to us later, including wireless numbers to market products and services.

i know i can, View and download the Campaign for Disability Employment's public service announcement “I Can,” along with complementary posters and discussion guides . Contact us to get started on the journey. We are committed to the best hope and future for your child and family. We have the the essential services you need.

Iowa College Access Network | ICAN, At ICAN – we support families as they work to advocate for themselves. As an organization, ICAN also advocates on behalf of the families in our communities� Minnesota State Parks and Trails I Can! programs take you and the kids from "I've never done that" to "Wow, that was a blast!" Designed for families to try any or all for the first time. Hands-on instruction from experienced and friendly crews.

International Campaign to Abolish Nuclear Weapons: ICAN, “Yes, I Can!” is a Super Simple song that introduces animal vocabulary, action verbs, and the phrases, “Can you___?” and “Yes, I can. / No, I can’t.”