Created
February 26, 2019 21:30
-
-
Save nickgs/2c7110118a7b2548d368621d22203212 to your computer and use it in GitHub Desktop.
Super simple example of a React component consuming a Drupal node exposed via the core REST functionality in Drupal 8.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
/** | |
* Super simple example of a React component consuming a Drupal node exposed via the core REST functionality in Drupal 8. | |
*/ | |
export default class DrupalNode extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
node: [] | |
} | |
} | |
componentDidMount() { | |
// Go out and grab the node we want to load from the REST API. | |
fetch('http://nostalgichugle.nick.mystack.co/node/' + this.props.nid + '?_format=hal_json') | |
.then(Response => Response.json()) | |
.then(res => { | |
console.log(res); | |
this.setState({ | |
node: res, | |
}); | |
}) | |
.catch(error => { | |
console.log(error) | |
}) | |
} | |
render() { | |
// Test to see if we have a node body. | |
if(this.state.node.body) { | |
return( | |
<p>{ this.state.node.body[0].value}</p> | |
) | |
} | |
else { | |
// If we haven't loaded it yet then render a loader. | |
return( | |
<p>Loading...</p> | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment