I am developing a React project & using AWS Amplify as a tool for serverless backend, in which I have a DynamoDB as my database in AWS Cloud.
My React app needs to detect realtime data update in DynamoDB. I am using GraphQL API from Amplify.
The GraphQL API generated by Amplify provides a subscriptions API for subscribing for data changes in real-time. I think that's exactly what I need, so I am using it:
import React, {useEffect, useState, useRef} from 'react';
import Amplify, { API, graphqlOperation } from 'aws-amplify';
import { onCreateData } from './graphql/subscriptions';
// subscribe to listen to new data creation in DynamoDB
const subscription = API.graphql(
graphqlOperation(onCreateData)
).subscribe({
next: (newData) => {
// New data is received here outside 'App' functional component,
// how can I render the data then?
console.log(`new data: ${JSON.stringify(newData)}`);
}
});
// I can't put the above subscription code inside App component, I don't know why but it just doesn't work unless I move it outside the App functional component.
function App() {
const [dataArray, setDataArray] = useState([]);
...
useEffect(() => {
fetchDataArray();
showDataArray();
return () => {
//cleanup
}
}, [dataArra])
return (<div>
<canvas ref={canvasRef}/>
</div>)
}
export default App;
Problem is the subscription for new data insertion in DynamoDB only work if I put it outside the function App(){...} as you can see above. So, when newData is received in the subscription callback next: (newData), how can I show the new data in App component?
You can see inside my App component I have state variable dataArray, the ideal solution is to update this dataArray state with newData but I think it is impossible now. So, in general I wonder how can I show the new data in my App component now?
P.S. Amplify GraphQL API doc sample code (if you see the "subscription" section) also shows that the subscription code is in the same level as imports.