I am importing some React modules from CDN (that's not a requirement, I've also tried with a local build, more in the final question about it):
<script crossorigin src="https://unpkg.com/[email protected]/dist/react-onclickoutside.min.js"></script>
<script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.0/moment.min.js" integrity="sha512-Izh34nqeeR7/nwthfeE0SI3c8uhFSnqxV0sI9TvTcXiFJkMd6fB644O64BRq2P/LA/+7eRvCw4GmLsXksyTHBg==" crossorigin="anonymous"></script>
<script crossorigin src="https://unpkg.com/[email protected]/dist/react-datepicker.min.js"></script>
Then I have a script to build the React DatePicker
component, this is the relevant snippet from it:
HelloWorld.Example=function()
{
var p,setCount,count,p$1,c,myDate,datePicker;
p=React$2.useState(0);
setCount=p[1];
count=p[0];
p$1=React$2.useState(new moment(new Date((c=Date.now(),DateUtil.DatePortion(c)))));
myDate=p$1[0];
datePicker=React$2.createElement(DatePicker.default,{
selected:new moment(new Date()),
onChange:p$1[1]
});
React.set_setCount(setCount);
return React$2.createElement("div",null,datePicker,React$2.createElement("p",null,(Html.textf(function($1)
{
The error that I see from the JS Console is:
react-datepicker.min.js:1 Uncaught TypeError: o is not a function
at Ee (react-datepicker.min.js:1)
when the script call ReactDOM.render
.
Is there a way to understand what is o
? Maybe an import missing?
(Edit Well, looking at chrome debugger and comparing it to github, o
is isValidDate
, i.e. import isValidDate from "date-fns/isValid";
, hence the imports from date-fns
are not working from CDN )
Is there a way such that - for example - I can locally npm run build
the needed module, react-datepicker
, and then call the react API from my script as shown above? (a suggestion that I received was configuring my script as entry in webpack
, but afaik React doesn't use webpack
, though I see it is used in react-datepicker
).
From React docs, I can read that
JSX is not a requirement for using React
so something like the above should be doable, in theory.
I've opened a question/issue on github react-datepicker
repo (in the context of calling this component from WebSharper.React
).
Yes, there is a well known solution!
Write an index.js as follows
build via
npm
and copy the static folder from the build of your bynpm run build
to the SPA folder of your projcopy the 3 script tags from the index.html in the build into the index.html template of your proj and
<div id="root"></div>
(of course you use a different id for your project app and there will be nothing to render here)in my case they are (they will be different for you)
Now go with
in you SPA.js and enjoy any react component like this one from WebSharper.React!
Btw I had to pass a JS date, not a Moment date here in the
selected
ofprops
, I'm not sure why, anyway, this is not relevant to the problem.FYI, this is the F# code from WebSharper project
Full open source project shared on github.