Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an ejs variable inside a react render function?

I'm passing a variable abcd to my index.ejs which in turn calls a react js file to render the index.ejs page. I've been able to access <%= abcd %> inside index.ejs but not inside the render function of react. Can you help me out?

Thanks

like image 734
Pratyush Vashisht Avatar asked Sep 25 '22 10:09

Pratyush Vashisht


1 Answers

I faced the same problem but with a index.html file, filled by a React component, and resolved it like this:

In index.html

<script>
    window.abcd = '<%- abcd %>';
</script>

In your react component

render: function(){
    return(
       <div>
            <p> {window.abcd} </p>
       </div>
    );
}

This worked perfectly for me, if someone has a cleaner proposition, i'm in!

like image 112
Made in Moon Avatar answered Sep 28 '22 06:09

Made in Moon