Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router doesn`t load external html files

I am building a React App with react + react-router, but I have a problem about the redirections.

I have the next folder structure:

... /public /index.html (react index) /tos/ /terms.html /privacy.html ... /src index.js App.js ...

And the index.js:

class App extends Component {

    render() {
        return (
            <Router>
                <Switch>
                    <Route exact path="/" component={Home}/>
                    <Route path="/settings" component={Settings}/>
                </Switch>
            </Router>
        )
    }
}

ReactDOM.render(<App/>, document.getElementById('root'));
registerServiceWorker();

The problem is when I want redirect to /public/tos/terms.html or /public/tos/privacy.html, sometimes (more frequently in chrome), I can not. The project render again the index, and show a blank page because the route /public/tos/terms.html or /public/tos/privacy.html is not declared in the Route Switch.

On the other hand, I have other "no-react" project in the same base_url, but listen in other port www.my-project.com:4040/other and I have configured it in the next route: www.my-project.com/other. But in this case, I have the same problem, when I redirect from my React-app to this route, react do no redirect and render again the App.js without components inside.

Somebody could tell me any way for redirect to other place in the same project but out of react?

like image 487
Raul Rulo Avatar asked Oct 30 '17 10:10

Raul Rulo


People also ask

How do you load external HTML file in react?

You can try react-templates. This is 'the' best available. You can have your template as an external file and can load it whenever you want and it'll render like charm with all the React API available.

How do you navigate to an external URL in react router?

You can use the Link component or an HTML anchor tag if you want to redirect to an external link with React Router. Using the < Link > component, you can do it thanks to the pathname and target parameters. It will redirect the user to the specified URL.

Is react Router deprecated?

This feature has been deprecated because the new structure of Routes is that they should act like components, so you should take advantage of component lifecycle methods instead.

What is the BrowserRouter /> component?

BrowserRouter: BrowserRouter is a router implementation that uses the HTML5 history API(pushState, replaceState and the popstate event) to keep your UI in sync with the URL. It is the parent component that is used to store all of the other components.


1 Answers

If your trying to reach valid url /public/tos/terms.html you must have component to take care of the url :

<Route  path="/public/tos/terms" component={/*Component to render*\ }/>

So it will be like :

        <Router>
          <div>
            <Switch>
                <Route path="/settings" component={Settings}/>
                <Route  path="/public/tos/terms" component={/*Component to render*\ }/>
                <Route exact path="/" component={Home}/>
            </Switch>
          </div>
        </Router>

If you are looking for how you can include your external files as a components inside your reactjs then one of the ways is , sending get request using axios and then you can use it as dangerouslySetInnerHTML ,

function getExternalFile(fileName) {
   const request = axios.get('/myAPI/getExternalFile/' + fileName).then(
    respone =>
     {this.setState({externalFile: response})}); }

function MyComponent() {
  return <div dangerouslySetInnerHTML={getHTMLFile()} />;
}
like image 188
Aaqib Avatar answered Oct 11 '22 10:10

Aaqib