I have a component inside of my React.js app, using Next.js - this component is the Header I'm using this inside of my layout to get every header element.
What I want to do is get an API request inside of this, so i can render the Menu items inside of the header.. the API is returning the Menu object.
And here is my code.
import fetch from 'isomorphic-unfetch'
import React, { Component } from "react";
import Link from 'next/link';
import { Config } from "../config.js";
class Header extends Component {
static async getInitialProps() {
const menuRes = await fetch(
`${Config.apiUrl}/wp-json/menus/v1/menus/header-menu`
);
const menu = await postsRes.json();
return {menu}
}
render() {
console.log(this.props);
return (
<div>
213
</div>
);
}
}
export default Header
Im still learning react so please excuse me if I'm 100% wrong on this. Just the Props doesn't return anything for:
this.props
I've not been able to find anyone talking about this online, otherwise I'd happily read a tut on it. Obviously this works on a page.js and returns all my items.
static async getInitialProps(context) {
const { id } = context.query
const postsRes = await fetch(
`${Config.apiUrl}/wp-json/wp/v2/pages?slug=sample-page`
);
const posts = await postsRes.json();
return {posts}
}
But I don't know why it wont work inside of the component :(