then can I access this data in my package which is react app? If it is simple javascript file then I can access but in react app is it possible?
It's still just JavaScript, anything's possible; but you may be doing something weird. This is an XY question. Tell us what you want to do instead of how.
I am creating react widget in which I am loading my widget on website using CDN script tag. Now this widget is react app. I want to pass token using data-* attribute to react app how can I do that ? script tag code: <script src="https://unpkg.com/mypackage@1.0.1/index.js" data-token="100200"></script> now how to access this token attribute in react app? I am using webpack with react app so I am generating single bundle.js file. Let me know if you need some more info I think this is sufficient
If it'll be a single bundle you can do document.currentScript.dataset.token
But in which file of my react app? Can I access it in app.js file?? Which file can access this token!?
Anywhere should work, saving it in a variable at top level would be best
Anywhere should work?? But could not understand how anywhere should work?? I was thinking it will be available only in app.js
It is bundled into a single script, yes?
Yes i am using webpack and it will generate single bundle.js file in dist folder
Then the entire file will be able to access document.currentScript
Oh ok got it. Let me try it out. Thanks for helping 😀
Just be sure to not use it from an event handler; since the element that triggers the event will be the target then. So in case you need to use it inside an event handler, assign it to a variable at the top level and pass it down
No in my case I am passing token as data attribute into my react app using CDN script tag and then once token is available in app.js I will display different components based on different tokens in react widget thats the plan. Anything you would like to suggest or looks good?
Обсуждают сегодня