# React integration example

**URL:** <https://devforum.okta.com/t/react-integration-example/800>\
**Category:** Questions\
**Tags:** reactjs\
**Created:** [November 23, 2017, 1:26pm UTC](https://devforum.okta.com/t/react-integration-example/800 "2017-11-23T13:26:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dziamianchyk](https://avatars.discourse-cdn.com/v4/letter/d/cdc98d/32.png) [@Dziamianchyk](https://devforum.okta.com/u/Dziamianchyk)\
**Post date:** [November 23, 2017, 1:26pm UTC](https://devforum.okta.com/t/react-integration-example/800/1 "2017-11-23T13:26:29Z")

</div>

Hello,  
I have made integration with OKTA on this example: [https://developer.okta.com/code/react/okta\_react\_sign-in\_widget.html](https://developer.okta.com/code/react/okta_react_sign-in_widget.html)

I set SecureRoute for home page inside “App” component:  
“SecureRoute path=’/’ exact={true} component={Home}”

As result:  
“Warning: setState(…): Cannot update during an existing state transition (such as within `render` or another component’s constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.”"

Further investigations led me to “onAuthRequired” method:  
onAuthRequired({ history }) {  
(“history.push(’/login’)”);  
}

which causes setState inside"render".

What is the correct solution to redirect unauthorized users to my custom “/login” page?

---

<div class="post-metadata">

**Author:** ![bbixler](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@bbixler](https://devforum.okta.com/u/bbixler)\
**Post date:** [February 1, 2018, 4:41am UTC](https://devforum.okta.com/t/react-integration-example/800/2 "2018-02-01T04:41:59Z")

</div>

@Dziamianchyk I am seeing this same issue as well. I’ve noticed that the sign on widget doesn’t load at times and requires the user to reload the page for it to render properly.

---

<div class="post-metadata">

**Author:** ![tom](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/tom/32/24_2.png) [@tom](https://devforum.okta.com/u/tom)\
**Post date:** [February 1, 2018, 3:40pm UTC](https://devforum.okta.com/t/react-integration-example/800/3 "2018-02-01T15:40:08Z")

</div>

What version of the widget are you using?

Is this something that is reproducible every time or is intermittent.

---

<div class="post-metadata">

**Author:** ![bbixler](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@bbixler](https://devforum.okta.com/u/bbixler)\
**Post date:** [February 1, 2018, 4:03pm UTC](https://devforum.okta.com/t/react-integration-example/800/4 "2018-02-01T16:03:08Z")

</div>

Using the latest SDK/widget. Essentially copy/pasted from the documentation.

**Steps to reproduce**

1. create-react-app new-app
2. Install latest Okta libraries
3. Follow Okta React documentation
4. Click any route for a secured resource (in my code below, this is /app/route) - WORKS (but throws console error message)
5. Click any unsecured resource (like / in my example below)
6. Click any secured resource (/app/route) - FAILS to render and throws same console message as above

Here is my app.js file, which you can use to reproduce the issue.

```
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import { Security, ImplicitCallback, SecureRoute } from '@okta/okta-react';
import Home from './pages/Home';
import Login from './pages/Login';
import AppHome from './app/Home';
import config from './config';

function customAuthHandler({ history }) {
  // Redirect to the /login page that has a CustomLoginComponent
  history.push('/login');
}

const App = () => (
  <Router>
    <Security
      issuer={config.oidc.issuer}
      client_id={config.oidc.clientId}
      redirect_uri={config.oidc.redirectUri}
      onAuthRequired={customAuthHandler}
    >

      <Switch>
        <Route path="/" exact component={Home} />
        <Route path="/callback" component={ImplicitCallback} />
        <Route path="/login" component={Login} />
        <SecureRoute path="/app/home" component={AppHome} />
      </Switch>
    </Security>
  </Router>
);
export default App;

```

The issue triggered by the customAuthHandler function. `history.push('/login')` is what is throwing the error (visible in console) and will cause the login widget to not reliably render without refreshing the page.

Thanks,

---

<div class="post-metadata">

**Author:** ![iskander.samerkhanov](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/iskander.samerkhanov/32/337_2.png) [@iskander.samerkhanov](https://devforum.okta.com/u/iskander.samerkhanov)\
**Post date:** [February 9, 2018, 9:13pm UTC](https://devforum.okta.com/t/react-integration-example/800/5 "2018-02-09T21:13:07Z")

</div>

I have the same warning when i use `history.push('/login');`

---

<div class="post-metadata">

**Author:** ![mschaeffner](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@mschaeffner](https://devforum.okta.com/u/mschaeffner)\
**Post date:** [March 7, 2018, 12:32am UTC](https://devforum.okta.com/t/react-integration-example/800/6 "2018-03-07T00:32:33Z")

</div>

I created an issue and pull request for the error:

Issue: [https://github.com/okta/okta-oidc-js/issues/138](https://github.com/okta/okta-oidc-js/issues/138)

PR: [https://github.com/okta/okta-oidc-js/pull/139](https://github.com/okta/okta-oidc-js/pull/139)

---

<div class="post-metadata">

**Author:** ![kdegtyarenko](https://avatars.discourse-cdn.com/v4/letter/k/839c29/32.png) [@kdegtyarenko](https://devforum.okta.com/u/kdegtyarenko)\
**Post date:** [May 20, 2020, 3:20pm UTC](https://devforum.okta.com/t/react-integration-example/800/7 "2020-05-20T15:20:19Z")

</div>

It seems that issue is there again. I’m using latest React and React Router.

---

<div class="post-metadata">

**Author:** ![zgtc](https://avatars.discourse-cdn.com/v4/letter/z/278dde/32.png) [@zgtc](https://devforum.okta.com/u/zgtc)\
**Post date:** [August 20, 2020, 9:55pm UTC](https://devforum.okta.com/t/react-integration-example/800/8 "2020-08-20T21:55:11Z")

</div>

This is happening to me as well. Seems like the recommended way is not following react-router-dom best practices?

react-router-dom@5.1.2  
@okta/okta-react@3.0.2  
@okta/okta-signin-widget@4.3.2

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [January 25, 2024, 6:22pm UTC](https://devforum.okta.com/t/react-integration-example/800/9 "2024-01-25T18:22:45Z")

</div>

Closed on January 25, 2024, 6:22pm UTC


