Search code examples
reactjsamazon-web-servicesamazon-polly

AWS polly (text to speech) in react js. cannot get the AWS context to give credentials


I try to implement the AWS polly in react js. I can include the script using useEffect. but i can't get the context of AWS. It is always return error AWS is not defined. how to resolve this issue

import React, {useEffect, useState} from 'react';

const Polly = (props) => {  
    useEffect(() => {
        const script = document.createElement('script');
        script.src = 'https://sdk.amazonaws.com/js/aws-sdk-2.410.0.min.js';
        script.onload = function(){setScriptLoaded();};
        script.async = true;
        document.body.appendChild(script);
        return document.body.removeChild(script);
    }, []);

    const setScriptLoaded = () => {
        console.log(AWS);
    }

    return (
        <p>AWS</p>
      )
}

export default Polly;

Solution

  • You should import AWS

    import AWS from 'aws-sdk';