Express – Detecting movement in a group reaction

How should I detect mobile devices in reactjs express? I use mobile-detect to determine whether to move or not but first I use const md = new MobileDetect(window.navigator.userAgent) to implement it, but I remember that the window does not exist when the server is loaded. The example in the express part should work because this is me The only place where req can be accessed, but how do I pass it in my react application for later use?

UPDATE

// app.js

...
import {routes} from './routes';
import {match, RouterContext} from'react-router';
import {renderToString} from'react-dom/server';

.. .
const app = express();
app.use(express.static('public'));

app.set('view engine','ejs' );

app.get('*', (req, res) => {
// routes is our object of React routes defined above
match({ routes, location: req.url }, (err, redirectLocation, props) => {
if (err) {// something went badly wrong, so 500 with a message
res.status(500).send (err.message);

} else if (redirectLocation) {// we matched a ReactRouter redirect, so redirect from the server
res.redirect(302, redirectLocation.pathname + redirectLocation. search);

} else if (props) {// if we got props, that means we found a valid component to render for the given route
const reducer = combineReducers(reducers) ;
const store = applyMiddleware(...middlewares)(createStore)(reducer);

...
const server = http.createServer(app);
const port = process.env.PORT || 3003;

server.listen(port);
server.on('listening', () => {
console .log('Listening on '+ port);
});

// client-render.js
import {routes} from'./routes';
import {Router, browserHistory} from'react-router'

ReactDOM.render(

{scrollTop(); handleNotifs(store)}} routes={routes} history={browserHistory} />
,
document.getElementById('app')
) ;

// routes.js
import AppComponent from'./components/app';
import IndexComponent from'./components/index';
.. .

const routes = {
path:'',
component: AppComponent,
childRoutes: [{
path:'/',
component: IndexComponent,
name:'home'
}, {...}]
}

By making my routing module an accept The function of the user agent, I completed what you requested. Then it only returns an array or nested routes. This is an example…

routes.js

module.exports = (userAgent) => {
const md = new MobileDetect(userAgent)
if (md) {// not sure on the syntax of MobileDetect?< br /> return {
// return mobile routes
}
} else {
return {
// return desktop routes
}
}
}

app.js

app.get('*', (req, res) => {
match({ routes: routes(req.headers['user-agent']), location: req.url }, (err, redirectLocation, props) => {
// continue as normal
}
}

Customer render.js

ReactDOM.render(

{scrollTop(); handleNotifs(store)}} history={browserHistory}>
{routes(navigator.userAgent)}

,
document.getElem entById('app')
);

In addition, using this method we can also pass the auth cookie to the route, so that if the route lags behind the login, the onEnter method can redirect.

p>

Hope this helps! BTW React-Router is the best!! I got creators from New York, they are top-notch.

How should I detect mobile devices in reactjs express? I use mobile-detect to determine whether to move or not but first I use const md = new MobileDetect(window.navigator.userAgent) to implement it, but I remember that the window does not exist when the server is loaded. The example in the express part should work because this is me The only place where req can be accessed, but how do I pass it in my react application for later use?

UPDATE

// app.js

...
import {routes} from './routes';
import {match, RouterContext} from'react-router';
import {renderToString} from'react-dom/server';

.. .
const app = express();
app.use(express.static('public'));

app.set('view engine','ejs' );

app.get('*', (req, res) => {
// routes is our object of React routes defined above
match({ routes, location: req.url }, (err, redirectLocation, props) => {
if (err) {// something went badly wrong, so 500 with a message
res.status(500).send (err.message);

} else if (redirectLocation) {// we matched a ReactRouter redirect, so redirect from the server
res.redirect(302, redirectLocation.pathname + redirectLocation. search);

} else if (props) {// if we got props, that means we found a valid component to render for the given route
const reducer = combineReducers(reducers); const store = applyMiddleware(...middlewares)(createStore)(reducer);

...
const server = http.createServer(app);
const port = process.env.PORT || 3003;

server.listen(port);
server.on('listening', () => {
console.log ('Listening on '+ port);
});

// client-render.js
import {routes} from'./routes';
import {Router, browserHistory} from'react-router'

ReactDOM.render(

{ scrollTop(); handleNotifs(store))) routes={routes} history={browserHistory} />
,
document.getElementById('app')
);< br />
// routes.js
import AppComponent from'./components/app';
import IndexComponent from'./components/index';
...< br />
const routes = {
path:'',
component: AppComponent,
childRoutes: [{
path:'/',
component: IndexComponent,
name:'home'
}, {.. .}]
}

By making my routing module a function that accepts user agents, I accomplished what you asked for. Then it Only return an array or nested routes. This is an example…

routes.js

module.exports = (userAgent) => {
const md = new MobileDetect(userAgent)
if (md) {// not sure on the syntax of MobileDetect?
return {
// return mobile routes< br /> }
} else {
return {
// return desktop routes
}
}
}

app. js

app.get('*', (req, res) => {
match({ routes: routes(req.headers['user-agent ']), location: req.url }, (err, redirectLocation, props) => {
// continue as normal
}
}

Customer render. js

ReactDOM.render(

{scrollTop(); handleNotifs (store))) history={browserHistory}>
{routes(navigator.userAgent)}

,
document.getElementById('app ')
);

In addition, using this method we can also pass the auth cookie to the router , So that if the routing is behind login, the onEnter method can redirect.

Hope this helps! BTW React-Router is the best!! I got creators from New York, they are first-rate.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4181 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.