-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do I have to know previously what will be my View dimensions? #34
Comments
I'm also facing this issue. @zygama have you been able to figure it out? |
Not yet... |
Has there been a solution for this yet? |
Has anyone found a solution to this yet? @zygama @ws7one @chinookng |
hey guys, I believe onLayout will resolve your issue. That event let you know view's dimensions. |
code example: import React from 'react';
import { BoxShadow } from 'react-native-shadow';
function getShadowSettings(width, height) {
return {
width: width,
height: height,
color: colors.third,
border: 6,
opacity: 0.1,
x: 0,
y: 1,
};
}
class ShadowBox extends React.Component {
constructor(props) {
super(props);
this.state = {
width: 0,
height: 0,
calculated: false,
};
}
render() {
const { width, height, calculated } = this.state;
const { children } = this.props;
const childrenWithProps = React.Children.map(children, child =>
React.cloneElement(child, {
onLayout: e => {
const { width, height } = e.nativeEvent.layout;
this.setState({ width, height, calculated: true });
},
}),
);
if (!calculated) {
return childrenWithProps;
}
const shadowSettings = getShadowSettings(width, height);
return <BoxShadow setting={shadowSettings}>{childrenWithProps}</BoxShadow>;
} |
@hardrese7 Thanks for the information and the code snippet. I'll try it later today and get back to you. |
@JamieCorkhill |
@hardrese7 I've tried the snippet, though it's completely messing up my layout. |
@canpoyrazoglu import React from 'react';
import { BoxShadow } from 'react-native-shadow';
import { View } from 'react-native';
export function getShadowSettings(width, height) {
return {
width: width,
height: height,
color: '#18304C',
border: 6,
opacity: 0.05,
x: 0,
y: 1,
};
}
class ShadowBox extends React.Component {
constructor(props) {
super(props);
this.state = {
width: 0,
height: 0,
calculated: false,
};
}
onLayout(e) {
const { width, height } = e.nativeEvent.layout;
this.setState({ width, height, calculated: true });
}
render() {
const { children, shadowSettings: customShadowSettings = {} } = this.props;
if (React.Children.count(children) < 1) {
throw Error('ShadowBox should have at least one child');
}
let childrenWithProps = children;
if (React.Children.count(childrenWithProps) > 1) {
childrenWithProps = <View>{children}</View>;
}
childrenWithProps = React.cloneElement(
React.Children.only(childrenWithProps),
{
onLayout: this.onLayout.bind(this),
},
);
const { width, height, calculated } = this.state;
if (!calculated) {
return childrenWithProps;
}
const shadowSettings = {
...getShadowSettings(width, height),
...customShadowSettings,
};
return (
<View {...this.props}>
<BoxShadow setting={shadowSettings}>{childrenWithProps}</BoxShadow>
</View>
);
}
} |
@hardrese7 nope, still messing it up (though comparably better than the first one). It also errors on line |
I think I'll give up on this library and just apply a lame stretched PNG shadow below my views. |
Hello,
all is in the title, I would like to know if for using this lib I need to know in advance what will be the height and the width of my View? Because it's not my case, my View can have elements added or deleted.
Is there a workaround for my case?
Thank you :)
The text was updated successfully, but these errors were encountered: