Using NativeBase as my UI framework, I am required to explicitly install react-native-svg as a dependency.
My solution is to use both "victory-native" and "victory". Since import statements cannot be dynamic/conditional in JS, this requires a bit of a workaround.
First workaround that I could not get working is to use aliasing in
babel.config.js to switch "victory-native" to "victory" for web only. This means you "yarn add victory-native victory" such that you have both modules, and in the appropriate file you write "import {VictoryChart, VictoryBar } from "victory-native". The "victory-native" part will change to "native" on web. Again, I couldn't get this to work.
Second, slightly less elegant, workaround is to use React Native's platform specific code solution. At the bottom of the link it explains the file.js and file.native.js solution. You can use that to expose the module dynamically
So for example I have two files: victory.ts and victory.native.ts (ts for typescript). Inside of victory.ts is the following:
import * as Victory from "victory";
export default Victory;
It's the same for victory.native.ts, except it references "victory-native" in the import statement.
In the component using Victory Charts I have the following...
import V from "@utils/victory"; // @utils is just an alias
// some other code...
function Chart() {
return (
<View style={styles.container}>
<V.VictoryChart width={350} theme={V.VictoryTheme.material}>
<V.VictoryBar data={data} x="quarter" y="earnings" />
</V.VictoryChart>
</View>
);
}