react-native and svgcode sample

2020-08-01 07:58发布

问题:

I've been able to launch the react-native "AwesomeProject" app (from the tutorial) in the simulator, and now I'd like to render SVG in a react-native app.

I tried replacing the contents of the return(...) statement with an SVG element that contains one SVG ellipse code snippet and then the simulator screen turned red (probably because Microsoft uses blue:)).

My guess is that the solution involves the WebView component in react-native, but it's not clear to me how to embed the SVG code in this component.

Btw, eventually I'd like to render these graphics effects in a react-native app: https://github.com/ocampesato/reactjs-graphics

Suggestions and code samples would definitely be appreciated:)

回答1:

A couple of days ago I opened this Github PR to allow you to manually set HTML in a WebView component. I don't see any reason you couldn't pass in SVG if you wanted. This might give you a starting point.

However, the HTML or SVG gets passed in as a string, it's not the same as using HTML or SVG in React for the web. For that, you might be interested in this Github issue which proposes allowing web-React inside React Native's WebView.



回答2:

I don't think you'll be able to do that for now because there are some limitation on the javascript libraries that you can use inside React Native. For now you can see the native component mapped from the documentation: http://facebook.github.io/react-native/docs/getting-started.html#content



回答3:

You might want to check out this as well: react-native-svg, on github.

There are examples on the page, on how to proceed. Good luck!