So, I've created a little react app with a login, which is sending an ajax request to a local flask server, which returns {token:TOKEN, user:{username:USERNAME,email:EMAIL}}
as JSON
.
However, as my react app will have multiple pages, I'm using ReactRouter, but I can't figure out how to access variables such as User.username
globally, in multiple React.createClass({})
instances.
Bear in mind that I'm doing all of this client side (until I move to react server side rendering)
Currently, I'm storing these types of variables in localStorage
but this means that they don't get automatically updated in the UI, without a url change, or a refresh.
const Admin = (props) => (
<div>
<h2>Admin</h2>
<p>Welcome, {localStorage.username}</p>
{props.children}
</div>
)
But what I would like to be able to do is
const Admin = (props) => (
<div>
<h2>Admin</h2>
<p>Welcome, {User.username}</p>
{props.children}
</div>
)
Routes
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="login" component={Login}>
<IndexRoute component={LoginForm}/>
<Route path="twofactor" component={twoFactor}/>
<Route path="backupcode" component={backupCode}/>
</Route>
<Route path="register" component={Register} onEnter={auth.auth_disallow}/>
<Route path="admin" onEnter={auth.required} component={Admin}>
<Route path="dashboard" component={Dashboard}>
<IndexRoute component={DashboardIndex}/>
</Route>
<Route path="settings" component={Settings}>
<IndexRoute component={SettingsIndex}/>
<Route path="changepassword" component={ChangePassword}/>
<Route path="editinfo" component={EditInfo}/>
</Route>
<Route path="/logout" onEnter={auth.logout}/>
</Route>
<Route path='*' component={NotFound}/>
</Route>
</Router>,
document.getElementById('app')
);