Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

javascript - ReactJS Load Function “is not a function”

I'm setting up a react-cookie npm package but trying to load cookies as to call users but getting a not a function. How do I set up this correctly?

This is what I had tried right now. When someone put in user id first-name and last-name it will go to http://192.168.1.163:3000/users/ page.

import React, { Component } from 'react';
import '../sass/main.scss';
import Navbar from 'react-bootstrap/Navbar';
import Nav from 'react-bootstrap/Nav';
import ModalLogin from '../components/Modal/Login';
import ModalSignup from '../components/Modal/Signup';
import { Cookies } from 'react-cookie';
import axios from 'axios';

class App extends Component {
    constructor() {
      super();
      // call user and that 
      var user = Cookies.load('user');
      var that= this;
      this.state = {name: null, signedIn: false}

      //  When someone put in user id first-name and last-name it will go to http://192.168.1.163:3000/users/ page
      if (user){
        axios.get('http://192.168.1.163:3000/users/' + user.id)
          .then(function (response) {
            console.log(response);
            that.setState({name: response.data.data.attributes['first-name'] + ' ' + response.data.data.attributes['last-name'], signedIn: true});
            //this.setState({ showModal: false });
          })
          .catch(function (error) {
            console.log(error);
          });
        }
      this.render.bind(this);
    }

    render() {
        return (
          <Navbar expand="lg">
              <Navbar.Brand href="#home">Kommonplaces</Navbar.Brand>
              <Navbar.Toggle aria-controls="basic-navbar-nav" />
              <Navbar.Collapse id="basic-navbar-nav">
                <Nav className="ml-auto">
                  <Nav.Link href="#activities">Activities</Nav.Link>
                  <Nav.Link href="#space">List Your Space</Nav.Link>
                  <Nav.Link className={this.state.signedIn === true ? 'hidden' : ''}>Sign up</Nav.Link>
                  <Nav.Link className={this.state.signedIn === true ? 'hidden' : ''}>Log in</Nav.Link>
                </Nav>
              </Navbar.Collapse>
          </Navbar>
        );
    }
}

export default App;```
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are probably looking for get method on Cookies

  var user = Cookies.get('user');

https://github.com/reactivestack/cookies/tree/master/packages/react-cookie#getname-options


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...