如何在Next.js的getInitialProps中获取Cookies?

huangapple go评论81阅读模式
英文:

How to get cookies in getinitial props in nextjs?

问题

如何在getInitialprops中获取cookies以从Next.js页面中获取API?

英文:

How to get cookies in getInitialprops to fetch api from page in nextjs?

答案1

得分: 1

你可以从 npm 安装 Next Cookies。以下是链接:

https://www.npmjs.com/package/next-cookies

要读取所有的 cookies,你可以使用:

const allCookies = cookies(ctx);

要读取单个 cookie:

const { myCookie } = cookies(ctx);

示例:

import React from 'react';
import cookies from 'next-cookies';

class NameForm extends React.Component {
  static async getInitialProps(ctx) {
    return {
      initialName: cookies(ctx).name || ''
    }
  }

  constructor(props) {
    super(props);
    this.state = { name: props.initialName || '' };
    this.handleChange = this.handleChange.bind(this);
    this.reset = this.reset.bind(this);
  }

  handleChange(event) {
    const newName = event.target.value;
    this.setState({ name: newName });
    document.cookie = `name=${newName}; path=/`;
  }

  reset() {
    this.setState({ name: '' });
    document.cookie = 'name=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT';
  }

  render() {
    return (
      <div>
        <p>Hi {this.state.name}</p>
        <p>Change cookie: <input
          type="text"
          placeholder="Your name here"
          value={this.state.name}
          onChange={this.handleChange}
        />!</p>
        <p>Delete cookie: <button onClick={this.reset}>Reset</button></p>
      </div>
    );
  }
}

export default NameForm;
英文:

You can install Next Cookies from npm. Here is link below

https://www.npmjs.com/package/next-cookies

To read all cookies you can use

const allCookies = cookies(ctx);

To read single cookies

const { myCookie } = cookies(ctx);

Example

import React from &#39;react&#39;
import cookies from &#39;next-cookies&#39;
class NameForm extends React.Component {
static async getInitialProps(ctx) {
return {
initialName: cookies(ctx).name || &#39;&#39;
}
}
constructor(props) {
super(props);
this.state = {name: props.initialName || &#39;&#39;};
this.handleChange = this.handleChange.bind(this);
this.reset = this.reset.bind(this);
}
handleChange(event) {
const newName = event.target.value;
this.setState({name: newName});
document.cookie = `name=${newName}; path=/`;
}
reset() {
this.setState({name: &#39;&#39;});
document.cookie = &#39;name=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT&#39;;
}
render() {
return (
&lt;div&gt;
&lt;p&gt;Hi {this.state.name}&lt;/p&gt;
&lt;p&gt;Change cookie: &lt;input
type=&quot;text&quot;
placeholder=&quot;Your name here&quot;
value={this.state.name}
onChange={this.handleChange}
/&gt;!
&lt;/p&gt;
&lt;p&gt;Delete cookie: &lt;button onClick={this.reset}&gt;Reset&lt;/button&gt;&lt;/p&gt;
&lt;/div&gt;
);
}
}
export default NameForm
</details>

huangapple
  • 本文由 发表于 2020年1月6日 20:25:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定