Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Team

I want to create a unique function that will return value back as data using axios and have local data as json file.But i need to call it on Sidebar.js to have access to the json file so can consume it.

What I have tried:

//Sidbar.js
JavaScript
<pre>import React,{Component, useEffect, useState} from 'react';
import axios from 'axios'

class Sidebar extends Component {

  // constructor.

  constructor(props) {
    super(props);
    this.state = { status: true };
  }

  render() {
    return (
      <div className="side">
      
      </div>
    );   
      
    }
  }
  
    export default Sidebar


// function for using axios
function Sidebar() {
  const [product, setProduct] = useState([]);

  useEffect(() => {
    axios
      .get("/Api/db.json")
      .then((res) => setUsers(res.data))
      .catch((err) => console.log(err));
  }, []);

  return (
    <div>
      <ul>
        {product.map((product, index) => (
          <li key={index}>
            {product.country} {product.title}
          </li>
        ))}
      </ul>
    </div>
  );
}

export default Sidebar;
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900