【问题标题】:Why am I getting ,, Cannot read property 'load' of undefined"为什么我得到,,无法读取未定义的属性“加载””
【发布时间】:2018-02-11 19:34:24
【问题描述】:

我已经像这样在我的项目中定义了 cookie。 从 'react-cookie' 导入 cookie。所以这意味着我已经定义了 cookie,所以我不应该收到那个错误,但我会得到它。如果有人认为需要它,我会分享我的代码。

编辑 我只在这里添加了一半的代码,因为我必须使用完整的代码添加更多细节而且我不知道要添加什么。完整代码可以在这里找到https://pastebin.com/APTdX879

    import React, { Component } from 'react';
    import logo from './logo.svg';
    import './App.css';
    import Block from "./Block"
    import Add from './Add'
    import cookie from 'react-cookie'

    class App extends Component {

      constructor(props){
        super(props)
        this.state = {
          logedin:  cookie.load("logedin")
        }
      }

      submit(){

        fetch("/newaccount")
        .then(res=>res.json())
        .then(data=>{
          console.log(data)
          console.log(document.getElementById('username').value)
          let arr = data.filter(event=>{

            return (event.username==document.getElementById('username').value&&event.password==document.getElementById('password').value)
          })
          console.log(arr.length)
          console.log(arr)
          if(arr.length==1){
            this.setState({logedin:true})
          }
          else{
            alert("wrong useranme or password")
          }
        })
      }

      componentDidMount(){
        fetch("/api")
        .then(response=>response.json())
        .then(data=> this.setState({data: data}))
        .catch(error=>console.log(error+' 1'))
      }
      componentWillMount() {

    }

      render() {
        if(!this.state.logedin){
          return (
            <div>
            <input id="username"/>
            <input id='password' type="password"/>
            <button onClick={this.submit.bind(this)}>Submit</button>
            </div>
            )
        }

        if(!this.state.data){
        return <p>loading</p>
      }
        return (

          <div>
          <hr/>
          <Add/>
          {
            this.state.data.map((data,i)=>{
            return(
              <div key={i+"a"}>
              <Block name={data.username} img={data.img} date={data.date} text={data.text} likes={data.likes} comments={data.comments} key={i} id={data._id}></Block>
            <br key={i+"b"}/>
            </div>
            )
          })
        }
          </div>

*这里的第二次编辑是错误http://prntscr.com/gghb5p的截图。

【问题讨论】:

  • 我们无法从描述中帮助您。请提供代码和确切的错误消息。您可以阅读stackoverflow.com/help-how-to-ask,了解如何让人们帮助您
  • 错误是什么,发生在哪一行代码
  • @Mikkel 我刚刚添加了一个包含错误信息的屏幕截图。

标签: reactjs cookies session-cookies


【解决方案1】:

先尝试注入:

import { withCookies } from 'react-cookie';

export default withCookies(App);

App = withCookies(App);

然后在你的构造函数中:

    this.state = {
      logedin:  props.cookies.get("logedin")
    }

编辑:react-cookie 的文档中没有加载方法,我认为您需要

logedin:  props.cookies.get("logedin")

【讨论】:

  • 编译失败。
  • @PetarPtolomejStojanov export default withCookies(App);,对不起
  • 当我添加默认值时。我得到了同样的错误。在进行更改之前。
  • 您是否将道具名称从 cookie 更改为 cookies - props.cookies.load ?如果你做 console.log(props); ?
  • 使用 get 代替 load 并将 cookie 更改为 cookies 。你得到同样的错误吗?。
【解决方案2】:

根据https://www.npmjs.com/package/react-cookie 的 react-cookie 文档,您应该像这样导入它:

import { CookiesProvider, withCookies, Cookies } from 'react-cookie';

然后在构造函数中获取 cookie 将不起作用,您需要在 componentWillMount() 方法中这样做:

  componentWillMount() {
    const { cookies } = this.props;

    this.state = {
      name: cookies.get('name') || 'Ben'
    };
  }

【讨论】:

  • 为什么在构造函数中获取 cookie 不起作用?
  • 跟时机有关。构造函数是你设置默认值的地方,像 componentWillMount 这样的方法稍后完成(就在渲染之前),这是你想要从环境中加载数据和状态的时候。这里有一篇关于它的文章供你阅读:busypeoples.github.io/post/react-component-lifecycle
  • 当我尝试添加你的代码时,我得到了。道具未定义。
  • 我只是从文档中剪切并粘贴了一些代码 - 你为什么不阅读它并看看它是如何工作的
猜你喜欢
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 2020-06-12
  • 1970-01-01
相关资源
最近更新 更多