G
G
GS-30182019-04-24 11:56:01
Node.js
GS-3018, 2019-04-24 11:56:01

How to output JSON to reactjs page?

Good day to all!
Faced a problem, it is not possible to display JSON data via api. The options that were offered here in similar questions also do not solve the problem. Perhaps some additional package is needed?

package.json
{
  "name": "react-ssr",
  "version": "0.1.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "start-dev": "NODE_ENV=development webpack -w & NODE_ENV=development node server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.4",
    "react": "^15.6.2",
    "react-dom": "^15.6.2",
    "react-fetch": "0.0.9",
    "react-router": "^5.0.0"
  },
  "devDependencies": {
    "autoprefixer": "^7.2.6",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-react-app": "^3.1.2",
    "bootstrap": "^4.3.1",
    "cors": "^2.8.5",
    "css-loader": "^0.28.11",
    "extract-text-webpack-plugin": "^2.1.2",
    "file-loader": "^0.11.2",
    "jquery": "^3.4.0",
    "mongoose": "^5.5.3",
    "mysql": "^2.17.1",
    "node-sass": "^4.11.0",
    "popper.js": "^1.15.0",
    "postcss-loader": "^2.1.6",
    "react-bootstrap": "^1.0.0-beta.8",
    "react-router-dom": "^5.0.0",
    "reactstrap": "^8.0.0",
    "sass-loader": "^7.1.0",
    "serialize-javascript": "^1.7.0",
    "style-loader": "^0.23.1",
    "webpack": "^3.12.0"
  }
}
Executable
import React, {Component} from 'react';

class Aktsii extends Component {
  constructor(props) {
    super(props);

    this.state = {
      items: [],
      isLoaded: false,
    }
      
  }
componentDidMount() {
    fetch('http://jsonplaceholder.typicode.com/users')
    .then(res => res.json())
    .then(json => {
    this.setState({
        isLoaded: true,
        items: json,
    })
    }
         
    )
}
  render() {
    var { isLoaded, items} = this.state
    if (!isLoaded) {
        return <div>load</div>
    }
    else {
    return (
    <div>{items.map(item => (
          <li key={item.id}>   
        Name: {item.name}
         </li>
          ))}</div>
    );
    }
  }
}

export default Aktsii;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-04-24
@GS-3018

isLoaded: false,

So how is it correct - Loaded or loaded?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question