Uh oh!
There was an error while loading. Please reload this page.
Event handler being reattached/recreated every new component render #1287
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I cannot figure this one out, so of course I'm finally turning here for help. I am running into an issue where the Below are the files to reproduce this minimal example. Any idea what's going on here? Is this a shortcoming of the way the Map component was designed? // example.jsimportReactfrom"https://esm.sh/react@19.0"importReactDOMfrom"https://esm.sh/react-dom@19.0/client"importMapfrom"https://esm.sh/@planet/maps@11.1.0/Map?deps=react@19.0,react-dom@19.0,react-is@19.0";export{Map};importViewfrom"https://esm.sh/@planet/maps@11.1.0/View?deps=react@19.0,react-dom@19.0,react-is@19.0";export{View};loadCSS("https://esm.sh/ol@10.4.0/ol.css");functionloadCSS(href){varhead=document.getElementsByTagName('head')[0];if(document.querySelectorAll(`link[href="${href}"]`).length===0){// Creating link element varstyle=document.createElement('link');style.id=href;style.href=href;style.type='text/css';style.rel='stylesheet';head.append(style);}}exportfunctionbind(node,config){constroot=ReactDOM.createRoot(node);return{create: (component,props,children)=>React.createElement(component,wrapEventHandlers(props), ...children),render: (element)=>root.render(element),unmount: ()=>root.unmount()};}functionwrapEventHandlers(props){constnewProps=Object.assign({},props);for(const[key,value]ofObject.entries(props)){if(typeofvalue==="function"){newProps[key]=makeJsonSafeEventHandler(value);}}returnnewProps;}functionstringifyToDepth(val,depth,replacer,space){depth=isNaN(+depth) ? 1 : depth;function_build(key,val,depth,o,a){// (JSON.stringify() has it's own rules, which we respect here by using it for property iteration)return!val||typeofval!='object' ? val : (a=Array.isArray(val),JSON.stringify(val,function(k,v){if(a||depth>0){if(replacer)v=replacer(k,v);if(!k)return(a=Array.isArray(v),val=v);!o&&(o=a?[]:{});o[k]=_build(k,v,a?depth:depth-1);}}),o||(a?[]:{}));}returnJSON.stringify(_build('',val,depth),null,space);}functionmakeJsonSafeEventHandler(oldHandler){// Since we can't really know what the event handlers get passed we have to check if// they are JSON serializable or not. We can allow normal synthetic events to pass// through since the original handler already knows how to serialize those for us.returnfunctionsafeEventHandler(){varfilteredArguments=[];Array.from(arguments).forEach(function(arg){if(typeofarg==="object"&&arg.nativeEvent){// this is probably a standard React synthetic eventfilteredArguments.push(arg);}else{filteredArguments.push(JSON.parse(stringifyToDepth(arg,3,(key,value)=>{if(key==='')returnvalue;try{JSON.stringify(value);returnvalue;}catch(err){return(typeofvalue==='object') ? value : undefined;}})))}});oldHandler(...Array.from(filteredArguments));};}# app.pyfromreactpyimportcomponent, html, run, webmodule=web.module_from_file(
"example", "example.js",
)
Map, View=web.export(module, ["Map", "View"])
@componentdefApp():
test, set_test=reactpy.hooks.use_state(True)
returnreactpy.html.div({"style": {"width": "100vw", "height": "100vh"}},
Map({"onClick": lambda_: print("CLICKED!") isNoneandset_test(nottest)},
View({"center": [-100, 40], "zoom": 3.5})
)
)
run(App) |
Replies: 2 comments 6 replies
What version of ReactPy are you running? I believe this issue was resolved in the latest alpha releases. |
I was able to reproduce this directly with React(JS), so I've opened up a ticket with the planetlabs/maps project here. |
I was able to reproduce this directly with React(JS), so I've opened up a ticket with the planetlabs/maps project here.