简体   繁体   English

为什么我的 React Native 组件不存在 this.props.onLayout?

[英]Why does this.props.onLayout not exist for my React Native component?

I have the following class in my React Native app (using Typescript):我的 React Native 应用程序(使用 Typescript)中有以下 class:

class Component1 extends React.Component<IntroProps, {}> {
  constructor(props){
    super(props)
    console.log(props.onLayout)
  }
  ...
}

I read in the documentation on the View class that any View or component that inherits from it have this.props.onLayout .我在View class 的文档中读到,从它继承的任何View或组件都具有this.props.onLayout However, this logs undefined , and if I use this.props.onLayout instead of props.onLayout , it gives the warning "Property 'onLayout' does not exist on type 'Readonly & Readonly<{ children?: ReactNode;>' ".但是,这会记录undefined ,如果我使用this.props.onLayout而不是props.onLayout ,它会给出警告“Property 'onLayout' does not exist on type 'Readonly & Readonly<{ children?: ReactNode;>'”。 I've seen other examples of people using this.props.onLayout in their component similar to mine, except that mine uses TypeScript.我已经看到其他人在他们的组件中使用this.props.onLayout的例子类似于我的,除了我的使用 TypeScript。

What I Want To Know:我想知道的:

Why can't I access this.props.onLayout here, and how can I access it?为什么这里访问this.props.onLayout ,怎么访问呢?

the onlayout belongs view, you can use as the following code: onlayout属于视图,可以使用如下代码:

class Component1 extends React.Component<IntroProps, {}> {
  constructor(props){
    super(props)
  }
  render() {
   <View onLayout={this.props.onLayout} style={[{},this.props.containStyle]}>
      <Image source={{ uri: 'http://fillmurray.com/200/200' }} width={200} 
     height={200} />
      <Text>My Picture</Text>
    </View>
  }
  ...
} 
class Example extends Component {
  //this is a method
  onLayout = (e) => {
    this.setState({
      width: e.nativeEvent.layout.width,
      height: e.nativeEvent.layout.height,
      x: e.nativeEvent.layout.x,
      y: e.nativeEvent.layout.y
    })
  }

  render (
    <Component1  onLayout={this.onLayout} containerStyle={width:this.state.width}/>
  )
}

you can look at this article , in some content, it show the React.componet source code and analysis it.你可以看看这篇文章,在一些内容中,它展示了 React.componet 源代码并对其进行了分析。 Actually,React.component is a fuction.实际上,React.component 是一个函数。

function ReactComponent(props, context, updater) {
  this.props = props;
  this.context = context;
  this.refs = emptyObject;
  // We initialize the default updater but the real one gets injected by the
  // renderer.
  this.updater = updater || ReactNoopUpdateQueue;
}

the more you want to learn, you can look at the article and read the source code.想学的越多,可以看文章看源码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 props 不能与我的子组件 React JS (react native) 一起使用 - Why props is not working with my child component React JS (react native) React Native Animated flatlist item expander 不会触发视图的 onLayout - React Native Animated flatlist item expander does not trigger onLayout of view 当我的屏幕第二次加载时,响应本机组件接收道具不加载 - react native component receive props does not load when my screen loaded for second time 在 React Native 视图上强制 onLayout - Forcing onLayout on React Native view React Native 声称我的图片不存在 - React native claims that my picture does not exist 当props进入组件时,为什么我的react组件没有重新渲染? - Why is my react component not re rendering when props comes into the component? 为什么我的 react native 组件不断刷新? - why my react native component keep refreshing? 如果 props 和 state 没有改变,为什么 React 组件会更新 - Why does React component update if props and state didn't change 为什么.current 和.props 在我的反应原生参考中总是未定义? - Why .current and .props are always undefined in my react native ref? 为什么在获取数据后更新 props 中传递的数组时,我的 React 子组件不会重新渲染? - Why does my React child component not re-render when an array passed down in props is updated after data fetch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM