-
-
Save sakibguy/a132d9ced3222413f1bedd3a426a84ba to your computer and use it in GitHub Desktop.
This file contains 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
lambda function | |
---- | |
exports.handler = async (event) => { | |
console.log(event) | |
const customerId = event.pathParameters.customerId; | |
const customer = {'customerId': customerId, 'customerName': "Customer " + customerId }; | |
const response = { | |
statusCode: 200, | |
// Uncomment below to enable CORS requests | |
headers: { | |
"Access-Control-Allow-Origin": "*", | |
"Access-Control-Allow-Headers": "*" | |
}, | |
body: JSON.stringify(customer), | |
}; | |
return response; | |
}; | |
index.js | |
--- | |
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import './index.css'; | |
import App from './App'; | |
import reportWebVitals from './reportWebVitals'; | |
import Amplify from "aws-amplify"; | |
import awsExports from "./aws-exports"; | |
Amplify.configure(awsExports); | |
ReactDOM.render( | |
<React.StrictMode> | |
<App /> | |
</React.StrictMode>, | |
document.getElementById('root') | |
); | |
// If you want to start measuring performance in your app, pass a function | |
// to log results (for example: reportWebVitals(console.log)) | |
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals | |
reportWebVitals(); | |
app.js | |
--- | |
import logo from './logo.svg'; | |
import './App.css'; | |
import Amplify, { API } from 'aws-amplify' | |
import React, { useEffect, useState } from 'react' | |
const myAPI = "YOUR API HERE" | |
const path = '/customers'; | |
const App = () => { | |
const [input, setInput] = useState("") | |
const [customers, setCustomers] = useState([]) | |
//Function to fetch from our backend and update customers array | |
function getCustomer(e) { | |
let customerId = e.input | |
API.get(myAPI, path + "/" + customerId) | |
.then(response => { | |
console.log(response) | |
let newCustomers = [...customers] | |
newCustomers.push(response) | |
setCustomers(newCustomers) | |
}) | |
.catch(error => { | |
console.log(error) | |
}) | |
} | |
return ( | |
<div className="App"> | |
<h1>Super Simple React App</h1> | |
<div> | |
<input placeholder="customer id" type="text" value={input} onChange={(e) => setInput(e.target.value)}/> | |
</div> | |
<br/> | |
<button onClick={() => getCustomer({input})}>Get Customer From Backend</button> | |
<h2 style={{visibility: customers.length > 0 ? 'visible' : 'hidden' }}>Response</h2> | |
{ | |
customers.map((thisCustomer, index) => { | |
return ( | |
<div key={thisCustomer.customerId}> | |
<span><b>CustomerId:</b> {thisCustomer.customerId} - <b>CustomerName</b>: {thisCustomer.customerName}</span> | |
</div>) | |
}) | |
} | |
</div> | |
) | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment