Develop/React Native

Parent View의 Height를 알고싶다면?

안다희 2020. 5. 4. 19:05
728x90

parent view의 속성 onLayout을 이용하면

event.nativeEvent.layout.height를 통해 height를 알 수 있다.

import React, {useState} from 'react';
import {View} from 'react-native';

export default ({routine, onMidBtnPress, children}: Props) => {
  const [parentHeight, setParentHeight] = useState(0);
  const onLayout = event => {
    const {height} = event.nativeEvent.layout;
    setParentHeight(height);
  };

  return (
    <View style={{width: 100, height: 100}} onLayout={onLayout}>
      <View
        style={{
          position: 'absolute',
          width: '100%',
          height: parentHeight,
          backgroundColor: 'pink',
          opacity: 0.5,
          alignSelf: 'center',
        }}
      />
    </View>
  );
};

 

 

출처

https://stackoverflow.com/questions/43067689/how-get-parent-size-in-child-view-in-react-native/43068063

 

how get parent size in child view in react-native?

I cannot understand how can I send parent view's sizes to child component of this. In renderPager() I want calculate some parameters that depend on parent view size. I know that we can do it through

stackoverflow.com

 

출처: https://mingos-habitat.tistory.com/34 [밍고의서식지:티스토리]