admin管理员组文章数量:1333442
I'd like to create an HOC for pages in my app that supplies some information kept in local storage for the case when the pages are not rendered on the server.
I am only able to actually set the data from ponentDidMount
because I need access to localStorage (not avail. in Node env. obviously).
import * as React from 'react'
const withLogin = Page => class SecurePage extends React.Component {
static async getInitialProps (ctx) {
let language;
if (ctx.req) {
language = ctx.req.session.language;
} else {
language = localStorage.getItem('language');
}
let props = {}
if (Page.getInitialProps) {
const pageProps = await Page.getInitialProps(ctx);
props = { ...pageProps, language }
}
console.log("this is what the props look like in the wrapping ponent", props)
return props;
// return Page.getInitialProps && { ...(await Page.getInitialProps(ctx)), language }
}
ponentDidMount () {
const { language } = this.props;
if (language) {
/* if we were able to fetch the language, set it here */
localStorage.setItem('language', language);
}
}
render () {
console.log("pre-render page props ", this.props);
return <Page {...this.props} />
}
}
export default withLogin
When I go to outfit a ponent with it, the HOC getInitialProps function runs as expected, but it has no impact on the getInitialProps function of the rendered ponent. Is there no way for me to pass props to the getInitialProps of a child ponent through HOC pattern?
class EmployerJobList extends React.Component {
static async getInitialProps(props) {
if (props.req) {
/* SSR */
// language = req.session.language;
} else {
/* non-SSR */
// language = user.preferred_language;
}
console.log("got language", props.language); // => undefined
getUrl (`/api/localization?filename=create-job&language=${props.language}`)
.then( jsonRes => {
console.log(jsonRes);
})
...
export default withLogin(EmployerJobList)
If this is not a viable pattern; is there any reliable way for me to share/reuse the logic I am aiming for here with localStorage?
I could theoretically do this in each Page ponent:
static async getInitialProps(props) {
let language;
if (props.req) {
/* SSR */
language = req.session.language;
} else {
/* non-SSR */
language = localStorage.getItem('language') // works
}
...
But that seems unnecessarily redundant.
I'd like to create an HOC for pages in my app that supplies some information kept in local storage for the case when the pages are not rendered on the server.
I am only able to actually set the data from ponentDidMount
because I need access to localStorage (not avail. in Node env. obviously).
import * as React from 'react'
const withLogin = Page => class SecurePage extends React.Component {
static async getInitialProps (ctx) {
let language;
if (ctx.req) {
language = ctx.req.session.language;
} else {
language = localStorage.getItem('language');
}
let props = {}
if (Page.getInitialProps) {
const pageProps = await Page.getInitialProps(ctx);
props = { ...pageProps, language }
}
console.log("this is what the props look like in the wrapping ponent", props)
return props;
// return Page.getInitialProps && { ...(await Page.getInitialProps(ctx)), language }
}
ponentDidMount () {
const { language } = this.props;
if (language) {
/* if we were able to fetch the language, set it here */
localStorage.setItem('language', language);
}
}
render () {
console.log("pre-render page props ", this.props);
return <Page {...this.props} />
}
}
export default withLogin
When I go to outfit a ponent with it, the HOC getInitialProps function runs as expected, but it has no impact on the getInitialProps function of the rendered ponent. Is there no way for me to pass props to the getInitialProps of a child ponent through HOC pattern?
class EmployerJobList extends React.Component {
static async getInitialProps(props) {
if (props.req) {
/* SSR */
// language = req.session.language;
} else {
/* non-SSR */
// language = user.preferred_language;
}
console.log("got language", props.language); // => undefined
getUrl (`/api/localization?filename=create-job&language=${props.language}`)
.then( jsonRes => {
console.log(jsonRes);
})
...
export default withLogin(EmployerJobList)
If this is not a viable pattern; is there any reliable way for me to share/reuse the logic I am aiming for here with localStorage?
I could theoretically do this in each Page ponent:
static async getInitialProps(props) {
let language;
if (props.req) {
/* SSR */
language = req.session.language;
} else {
/* non-SSR */
language = localStorage.getItem('language') // works
}
...
But that seems unnecessarily redundant.
Share Improve this question asked Jan 16, 2019 at 5:05 Daniel ThompsonDaniel Thompson 2,3514 gold badges25 silver badges38 bronze badges1 Answer
Reset to default 7Was able to pass values into the child's getInitialProps
function by supplying them as arguments:
static async getInitialProps (ctx) {
let language;
if (ctx.req) {
language = ctx.req.session.language;
} else {
language = localStorage.getItem('language');
}
let props = {}
if (Page.getInitialProps) {
const pageProps = await Page.getInitialProps(ctx, language); // <--- this
props = { ...pageProps, language }
}
return props;
}
Then in the child ponent's getInitialProps I could access the new argument:
static async getInitialProps({ query }, language) {
console.log("is language passed through: ", language); // "en"
const jobStatus = query.jobStatus;
return { jobStatus }
}
本文标签: javascriptnextjs pass props from HOC to child components getInitialPropsStack Overflow
版权声明:本文标题:javascript - next.js pass props from HOC to child components getInitialProps - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742354496a2459143.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论