Why am I not rendering my state to the screen?

Sofia Cole

Second time posting on stackoverflow :) Hopefully I won't be too vague.

I'm currently in the process of learning react and am really struggling. Today I am trying to figure out how to render the state (in the code it is this.state.feelings) to the screen.

Everything is console.logging the correct information, so I know the state HAS changed. I'm just not sure why it's not rendering.

import React, { Component } from 'react';
import getFeelings from '../calls/createStatsFeelings.js'
import API from "../../util/axiosApi.js";

class Stats extends Component {
  state = {
    feelings:''
  };

  componentDidMount() {
    this.getFeelings();
  }

  getFeelings = () => {
      API.getFeelings()
        .then( res =>
          {
            return this.setState({ feelings: res.data[0].feeling }, function 
() {console.log(this.state.feelings)})
          }
      )
        .catch(err => console.log(err));
      };

  render() {
    return (
       <div className='statsDiv'>
        <h1> {this.state.feelings} </h1>
      </div>
    )
  }
}


export default Stats;

Any idea what I'm doing wrong?

more code:

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import logo from './logo.svg';
import './App.css';
import Navbar from './components/navbar/Navbar.js';
import Login from './components/login/Login.js';
import Register from './components/register/Register.js';
import Goals from './components/goals/Goals.js';
import Stats from './components/stats/Stats.js';
import Update from './components/update/Update.js';
import Dashboard from './components/dashboard/Dashboard.js';
import DashboardReasons from './components/reasons/Reasons2.js';

// Stating 'extends Component' has nothing to do with the children
// It is extending the capability of the class being declared
// class App is declared with the extended ability of component
class App extends Component {
  render() {
    return (
      <Router>
        <div>
          <Navbar />
          <Switch>
            <Route exact path="/" component={Login} />
            <Route exact path="/register" component={Register} />
            <Route exact path="/dashboard" component={Dashboard} />
            <Route exact path="/goals" component={Goals} />
            <Route exact path="/update" component={Update} />
            <Route exact path="/stats" component={Stats} />
            <Route exact path="/reasons" component={DashboardReasons} />
            {/* TODO - NoMatch Route */}
          </Switch>
        </div>
      </Router>
    );
  }
}
Sidney

The first file you posted works fine.

Here it is, slightly modified to work in a browser:

const api = {
  getFeelings() {
    return Promise.resolve({
      data: [
        { feeling: 'test' }
      ]
    })
  }
}

class Stats extends React.Component {
  state = {
    feelings: ''
  };

  componentDidMount() {
    this.getFeelings();
  }

  getFeelings = () => {
    api.getFeelings()
      .then(res => {
        return this.setState({ feelings: res.data[0].feeling }, function () {
          console.log(this.state.feelings)
        })
      })
      .catch(err => console.log(err));
  };

  render() {
    return (
      <div className='statsDiv'>
        <h1> {this.state.feelings} </h1>
      </div>
    )
  }
}

const div = document.createElement('div')
document.body.appendChild(div)
ReactDOM.render(<Stats />, div)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

You'll need to post the rest of your code for more help. (Please don't add any more files to your question, create the app online using something like https://stackblitz.com/ and add the link here).

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why does my React app menu open when I am only changing the state and am not using any CSS?

分類Dev

Why am I getting a black screen with this opengl application?

分類Dev

why I am unable to publish my package to npmjs

分類Dev

Why I am getting Unresolved variable or type approvedPriceDealerCostForm in my HTML?

分類Dev

Why am I having to terminate my program manually?

分類Dev

Why am I not being able to connect to my database?

分類Dev

Why am I receiving negative values for my hash function?

分類Dev

My files are writeable, why am I seeing filesize() and fread() errors?

分類Dev

Why am I getting column not found in my SQL query?

分類Dev

How can I stop my change notifier provider from rebuilding my parent material app when I am rendering my child material app?

分類Dev

why am I getting a none value in my method inside of my model with nested if?

分類Dev

Why am I getting undefined from my onClick event on my anchor tag in React?

分類Dev

I am trying to search up to the right diagonally in my multidimensional array in java. Why am I getting index out of bounds error?

分類Dev

Why does my Dijkstra algorithm work for negative weights? Did I implement it incorrectly and am I sacrificing runtime?

分類Dev

Why am I able to use my value constructor even though I don't export it?

分類Dev

Why I am able to override Equals method if my class doesn't inherit from anything?

分類Dev

PHP - Why am I being warned that my regular expression is too large?

分類Dev

Using alpine source, why am I not able to source or "." my env file?

分類Dev

In react-native, why am I unable to define the width of my component?

分類Dev

The touch event in my canvas is not working as intended and I am not able to identify the reason why it is not working

分類Dev

Why is that when I am scrolling up or down the list it activates a Switch listener in my custom cursor adapter?

分類Dev

Why am I always getting Segmentation faults when displaying my array in C?

分類Dev

Why am I unable to use my Windows 8 after installing Windows 7 in a separate partition?

分類Dev

I am writing a JSP page and why my request.getAttribute return null?

分類Dev

Why am I getting an undefined?

分類Dev

Why am I getting this KeyError?

分類Dev

Why my React component is still updating state even if I do cleanup in useEffect()?

分類Dev

Why is my Angular 2 router not rendering my component?

分類Dev

Why is my code not rendering a list of table on my web page?

Related 関連記事

  1. 1

    Why does my React app menu open when I am only changing the state and am not using any CSS?

  2. 2

    Why am I getting a black screen with this opengl application?

  3. 3

    why I am unable to publish my package to npmjs

  4. 4

    Why I am getting Unresolved variable or type approvedPriceDealerCostForm in my HTML?

  5. 5

    Why am I having to terminate my program manually?

  6. 6

    Why am I not being able to connect to my database?

  7. 7

    Why am I receiving negative values for my hash function?

  8. 8

    My files are writeable, why am I seeing filesize() and fread() errors?

  9. 9

    Why am I getting column not found in my SQL query?

  10. 10

    How can I stop my change notifier provider from rebuilding my parent material app when I am rendering my child material app?

  11. 11

    why am I getting a none value in my method inside of my model with nested if?

  12. 12

    Why am I getting undefined from my onClick event on my anchor tag in React?

  13. 13

    I am trying to search up to the right diagonally in my multidimensional array in java. Why am I getting index out of bounds error?

  14. 14

    Why does my Dijkstra algorithm work for negative weights? Did I implement it incorrectly and am I sacrificing runtime?

  15. 15

    Why am I able to use my value constructor even though I don't export it?

  16. 16

    Why I am able to override Equals method if my class doesn't inherit from anything?

  17. 17

    PHP - Why am I being warned that my regular expression is too large?

  18. 18

    Using alpine source, why am I not able to source or "." my env file?

  19. 19

    In react-native, why am I unable to define the width of my component?

  20. 20

    The touch event in my canvas is not working as intended and I am not able to identify the reason why it is not working

  21. 21

    Why is that when I am scrolling up or down the list it activates a Switch listener in my custom cursor adapter?

  22. 22

    Why am I always getting Segmentation faults when displaying my array in C?

  23. 23

    Why am I unable to use my Windows 8 after installing Windows 7 in a separate partition?

  24. 24

    I am writing a JSP page and why my request.getAttribute return null?

  25. 25

    Why am I getting an undefined?

  26. 26

    Why am I getting this KeyError?

  27. 27

    Why my React component is still updating state even if I do cleanup in useEffect()?

  28. 28

    Why is my Angular 2 router not rendering my component?

  29. 29

    Why is my code not rendering a list of table on my web page?

ホットタグ

アーカイブ