Comments (10)
Cant do that in react-native. SVG like other components in react-native. It can
t set dimensions style in percent.
from react-native-svg.
I have a new PR, with support for nesting svg, percentages in width&height, and correctly re/rendering when changing dimensions: #596
Needs more testing though, just a fast first attempt.
from react-native-svg.
I hit this problem too. And FWIW, I've had no problem specifying height and width using percents in other React Native components. The problem with the Svg
element here is that it deliberately sanitizes the height and width parameters, with this code in render()
:
const {opacity, width, height, viewBox, preserveAspectRatio, style, ...props} = this.props;
let dimensions;
if (width && height) {
dimensions = {
width: +width,
height: +height,
flex: 0
};
}
I'm guessing that the +
coerces the value to a number, making percent values into NaN
. I found that if I removed the +
everything worked as expected. I expect RN to have standardized sanitization for width and height values, and I'm not sure why the Svg
element feels the need to do anything additional.
from react-native-svg.
Apparently, support came about a year ago: facebook/react-native@3f49e74
Feel free to make a PR, otherwise I'll come around to changing this after testing it out.
from react-native-svg.
@msand This give man a cookie!
from react-native-svg.
@msand percentages in width & height work on iOS, but on Android I'm seeing this error:
SurfaceView should have explicit width and height set
Any ideas?
from react-native-svg.
@mlazari Are you using Expo? I think they have an earlier version, from before the percent support was merged.
from react-native-svg.
@msand No, I don't use Expo. I had version 6.2.2 and updated now to 6.3.1, but still seeing the error. I tried to reproduce it in a new sample project, but there it works, so it must be something in my project.
from react-native-svg.
Have you tried cleaning the android build?
cd android
gradlew clean
from react-native-svg.
@msand Cleaning the project doesn't change anything. I can reproduce the error message in a new project with something like this:
render() {
return (
<View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
<View style={{ flex: 1 }}>
<MySVGComponent style={{ width: '50%', height: '100%' }} />
</View>
</View>
);
}
On iOS it's simply not visible (because the inner View has width 0), but doesn't show any errors. On Android it's showing "SurfaceView should have explicit width and height set" error.
In my project the width / height of the container is not zero, but I get the same error on Android. On iOS it works fine and the svg component is visible and has correct size.
from react-native-svg.
Related Issues (20)
- Exception thrown while executing UI block: -[RNSVGSvgView setOnClick:]: unrecognized selector sent to instance 0x10840d340 HOT 1
- Usage of std::stof crashes on Windows when using some non-English languages/locales (i.e. German / de) HOT 2
- `translateY` isn't working in react-native-web
- `Module parse failed` error when building for Storybook for web
- React Native Svg Linear Gradient Not loading when redirecting to bottom navigation tab HOT 1
- [Error: Expected > (0:110). If this is valid SVG, it's probably a bug. Please raise an issue HOT 3
- toDataURL only works the second time it is executed in old Arch HOT 1
- [Invariant Violation: requireNativeComponent: "RNSVGSvgView" was not found in the UIManager. HOT 7
- onPress in Path don't works on iOS, on android works
- The onPress method in the Path component works on android but not on ios.
- Svg Image pointerEvents="none" not working on Android
- Invalid `RNSVG.podspec` file: undefined method `visionos' for #<Pod::Specification name="RNSVG">. HOT 11
- Invariant Violation: requireNativeComponent: "RNSVGPath" was not found in the UIManager. HOT 2
- Fatal Exception: java.lang.Error Invalid number formating character 'N'
- SvgUri can not load some of the svgs in both android and ios
- Animating the strokeDashArray prop of an animated Path crashes running on the Android Simulator
- <Text /> with PanResponder only works if you click exactly on the letters
- Failed parsing SvgCssUri for first time, after reopening app it appears
- Xlinkhref only work on web does not work on ios and android (the color did not get inherited ) HOT 2
- React native web + vitest issue - Cannot use import statement outside a module HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-svg.