Ad
Next JS Custom Server App.render Do Not Pass Query To A Component
I'm trying to pass userData
with app.render
, but while Server side renderingrouter.query
is empty, although i have passed userData
! Is it NextJS's bug, or am i doing something wrong?
app.js:
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
if (pathname === '/index') {
app.render(req, res, '/index', {
userData: {
id: 1,
name: 'admin'
}
})
} else {
handle(req, res, parsedUrl)
}
}).listen(3333, err => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})
pages/index.js:
import { useRouter } from 'next/router'
export default () => {
const router = useRouter();
const { query } = router;
return (
<div>
Query: {JSON.stringify(query)}
</div>
);
};
Ad
Answer
If getInitialProps is absent, Next.js will statically optimize your page automatically by prerendering it to static HTML. During prerendering, the router's query object will be empty since we do not have query information to provide during this phase. Any query values will be populated client side after hydration.
You can access your query using getInitialProps
.
with useRouter
:
import { useRouter } from 'next/router'
const Index = () => {
const router = useRouter();
const { query } = router;
return (
<div>
Query: {JSON.stringify(query)}
</div>
);
};
Index.getInitialProps = async () => {
return {};
};
export default Index
with a class component:
import React from 'react'
class Index extends React.Component {
static async getInitialProps (context) {
let query = context.query;
return {query}
}
render() {
let {query} = this.props
return (
<div>
Query: {JSON.stringify(query)}
</div>
);
}
}
export default Index
Or if you prefer a functional component :
const Index = (props) => (
<div>
Query: {JSON.stringify(props.query)}
</div>
)
Index.getInitialProps = async ( context ) => {
let query = context.query;
return {query}
}
export default Index
Please note that obviously this works with /index
but not with /
Ad
source: stackoverflow.com
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error
Ad