I need to get the variables in my routes to update from the JSON data and also sometimes emit data, but I haven’t figured out how to pass response
to my Routes for accessing.
App.js file where the JSON is retrieved:
JavaScript
x
44
44
1
import React, { useState, useEffect } from 'react';
2
import { Router } from 'react-router-dom';
3
import { createBrowserHistory } from 'history';
4
import { ThemeProvider } from '@material-ui/styles';
5
import validate from 'validate.js';
6
7
import theme from './theme';
8
import 'react-perfect-scrollbar/dist/css/styles.css';
9
import './assets/scss/index.scss';
10
import validators from './common/validators';
11
import Routes from './Routes';
12
13
import socketIOClient from "socket.io-client";
14
const ENDPOINT = "http://127.0.0.1:5000";
15
16
const browserHistory = createBrowserHistory();
17
18
validate.validators = {
19
validate.validators,
20
validators
21
};
22
23
function App() {
24
const [response, setResponse] = useState("");
25
26
useEffect(() => {
27
const socket = socketIOClient(ENDPOINT);
28
socket.emit('test', {"name": "jim"})
29
socket.on("test_client", data => {
30
setResponse(data);
31
});
32
}, []);
33
34
return (
35
<ThemeProvider theme={theme}>
36
<Router history={browserHistory}>
37
<Routes />
38
</Router>
39
</ThemeProvider>
40
);
41
}
42
43
export default App;
44
Routes.js where the Routes are created:
JavaScript
1
81
81
1
import React from 'react';
2
import { Switch, Redirect } from 'react-router-dom';
3
4
import { RouteWithLayout } from './components';
5
import { Main as MainLayout, Minimal as MinimalLayout } from './layouts';
6
7
import {
8
Login as LoginView,
9
Dashboard as DashboardView,
10
Trading as TradingView,
11
OrderHistory as OrderHistoryView,
12
Account as AccountView,
13
Settings as SettingsView,
14
NotFound as NotFoundView
15
} from './views';
16
17
const Routes = () => {
18
return (
19
<Switch>
20
<Redirect
21
exact
22
from="/"
23
to="/dashboard"
24
data={props.response}
25
/>
26
<RouteWithLayout
27
component={LoginView}
28
exact
29
layout={MinimalLayout}
30
path="/login"
31
data={props.response}
32
/>
33
<RouteWithLayout
34
component={DashboardView}
35
exact
36
layout={MainLayout}
37
path="/dashboard"
38
data={props.response}
39
/>
40
<RouteWithLayout
41
component={TradingView}
42
exact
43
layout={MainLayout}
44
path="/trading"
45
data={props.response}
46
/>
47
<RouteWithLayout
48
component={OrderHistoryView}
49
exact
50
layout={MainLayout}
51
path="/orderhistory"
52
data={props.response}
53
/>
54
<RouteWithLayout
55
component={AccountView}
56
exact
57
layout={MainLayout}
58
path="/account"
59
data={props.response}
60
/>
61
<RouteWithLayout
62
component={SettingsView}
63
exact
64
layout={MainLayout}
65
path="/settings"
66
data={props.response}
67
/>
68
<RouteWithLayout
69
component={NotFoundView}
70
exact
71
layout={MinimalLayout}
72
path="/not-found"
73
data={props.response}
74
/>
75
<Redirect to="/not-found" />
76
</Switch>
77
);
78
};
79
80
export default Routes;
81
- I get the JSON data from my Flask backend using SocketIO
- JSON data is saved in response (I believe)
- I then want the JSON data to be accessible by each Route in Routes (the pages).
Any help is appreciated, thank you.
Advertisement
Answer
Pass props along:
App.js:
JavaScript
1
2
1
<Routes data={response} />
2
Routes.js:
JavaScript
1
12
12
1
const Routes = ({ data }) =>
2
3
<RouteWithLayout
4
// pass props here
5
component={routeProps => <LoginView {routeProps} data={data} />}
6
exact
7
layout={MinimalLayout}
8
path="/login"
9
/>
10
11
}
12