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

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

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,你可以使用:

  1. const allCookies = cookies(ctx);

要读取单个 cookie:

  1. const { myCookie } = cookies(ctx);

示例:

  1. import React from 'react';
  2. import cookies from 'next-cookies';
  3. class NameForm extends React.Component {
  4. static async getInitialProps(ctx) {
  5. return {
  6. initialName: cookies(ctx).name || ''
  7. }
  8. }
  9. constructor(props) {
  10. super(props);
  11. this.state = { name: props.initialName || '' };
  12. this.handleChange = this.handleChange.bind(this);
  13. this.reset = this.reset.bind(this);
  14. }
  15. handleChange(event) {
  16. const newName = event.target.value;
  17. this.setState({ name: newName });
  18. document.cookie = `name=${newName}; path=/`;
  19. }
  20. reset() {
  21. this.setState({ name: '' });
  22. document.cookie = 'name=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT';
  23. }
  24. render() {
  25. return (
  26. <div>
  27. <p>Hi {this.state.name}</p>
  28. <p>Change cookie: <input
  29. type="text"
  30. placeholder="Your name here"
  31. value={this.state.name}
  32. onChange={this.handleChange}
  33. />!</p>
  34. <p>Delete cookie: <button onClick={this.reset}>Reset</button></p>
  35. </div>
  36. );
  37. }
  38. }
  39. 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

  1. const allCookies = cookies(ctx);

To read single cookies

  1. const { myCookie } = cookies(ctx);

Example

  1. import React from &#39;react&#39;
  2. import cookies from &#39;next-cookies&#39;
  3. class NameForm extends React.Component {
  4. static async getInitialProps(ctx) {
  5. return {
  6. initialName: cookies(ctx).name || &#39;&#39;
  7. }
  8. }
  9. constructor(props) {
  10. super(props);
  11. this.state = {name: props.initialName || &#39;&#39;};
  12. this.handleChange = this.handleChange.bind(this);
  13. this.reset = this.reset.bind(this);
  14. }
  15. handleChange(event) {
  16. const newName = event.target.value;
  17. this.setState({name: newName});
  18. document.cookie = `name=${newName}; path=/`;
  19. }
  20. reset() {
  21. this.setState({name: &#39;&#39;});
  22. document.cookie = &#39;name=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT&#39;;
  23. }
  24. render() {
  25. return (
  26. &lt;div&gt;
  27. &lt;p&gt;Hi {this.state.name}&lt;/p&gt;
  28. &lt;p&gt;Change cookie: &lt;input
  29. type=&quot;text&quot;
  30. placeholder=&quot;Your name here&quot;
  31. value={this.state.name}
  32. onChange={this.handleChange}
  33. /&gt;!
  34. &lt;/p&gt;
  35. &lt;p&gt;Delete cookie: &lt;button onClick={this.reset}&gt;Reset&lt;/button&gt;&lt;/p&gt;
  36. &lt;/div&gt;
  37. );
  38. }
  39. }
  40. export default NameForm
  41. </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:

确定