create statically generated message object from jsx
While we use statically generated javascript code, we can't simply create Message object from plain object due to missing fromObject method (as the reverse of toObject)
See more in this issue
Before official support for fromObject method, we can use protobuf-jsx as a friendly workaround.
npm i -S protobuf-jsx- Enable
jsxintsconfig.json
{"compilerOptions":{"jsx": "react", "jsxFactory": "Protobuf.createMessage" } }If you don't use TypeScript, check out how to register jsx factory for your tool chain.
- Write
jsxin your javascript file.
importProtobuffrom'../src';import{Feature,Point}from'./fixtures/static_codegen/route_guide_pb';constfeature: Feature=(<Featurename="hello"location={<Point{...point}/>}></Feature>);// or using children render functionconstfeature: Feature=(<Featurename="hello">{()=>({location: <Point{...point}/>})}</Feature>);The following configurations prevent variables used in JSX to be marked as unused.
rules: react/jsx-uses-vars: 2plugins: - react```