This tutorial uses AWS SAM to create a hello-world Serverless app with API Gateway, Lambda, and DynamoDB.
Start with the first commit. Then Checkout
the next commit when you're ready to move onto the next step.
This commit renders the list of names returned from DynamoDB.
class App extends Component {
// 1
constructor() {
super();
this.state = {items: []};
}
componentDidMount() {
fetch(endpoint)
.then((response) => response.json())
.then((responseJson) => {
console.log("data: " + JSON.stringify(responseJson));
// 2
this.setState({items: responseJson.Items});
});
}
render() {
// 3
const rows = this.state.items.map((item, index) => <tr><td>{item.name}</td></tr>);
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<table>
<tbody>
// 4
{rows}
</tbody>
</table>
</div>
);
}
}
Here are the changes:
- The
state
is initially set to an object with anitems
property (which starts off with an empty array.) - When the data comes back from the API call, the
Items
payload is extracted. - Each name from the list is wrapped with a table row.
- The table rows of names goes into the table body.
The lifecycle kind of goes like this:
constructor()
gets called at the beginning.componentDidMount()
is called next. This kicks off the API call.render()
gets called when the page is initially loaded (which at this point has an empty array of names). It gets called again in the API callback, viasetState()
-- but this time, it has real data.
To deploy your ReactJS website to S3:
npm run deploy
Now go to the Outputs
tab of the CloudFormation stack, and click on the URL:
http://sam-tutorial-dev-s3bucket-s64qw5dvivdx.s3-website-us-east-1.amazonaws.com
Open the console, and verify that data is logged to the console.
If there is no table data, go into DynamoDB and create a few names, and reload the page.
Checkout
the next commit when you're ready to move onto the next step.