【问题标题】:Is the rendering of a child component in react different from react native?react中子组件的渲染与react native不同吗?
【发布时间】:2020-01-14 11:23:32
【问题描述】:

首先,我有一个基本的英语水平。所以希望你能理解。

React Native 中子组件的重新渲染是否有所不同? 举个例子吧:

class App extends React.Component{

 constructor(props) {
    super(props);

  }

 render(){
    return(
        <ChildComponent />
    )
  }

  componentDidMount() {
    this.setState({test:'test'})
  }

}    

这在反应中重新渲染 ChildComponent 一次,但在 React Native ChildComponent 中不重新渲染只有您的初始渲染。为什么?

ps:ChildComponent 在 React 中只有一个 div,在 React Native 中只有一个 Text。

【问题讨论】:

  • 你的意思是在 React-native 上看不到 ChildComponent 中的文字?
  • 我可以。但是如果我在 ChildComponent 的渲染方法中放置了一个 console.log('test'),在反应时我会看到两次'teste',并且在本机反应一次。欠债不是显示的文本,而是调用 ChildComponent 的方法渲染的次数。

标签: reactjs react-native render rerender


【解决方案1】:

在 react 和 react native 中没有渲染子组件是相同的。举个例子吧

父.js

import React, { Component } from 'react';
import { Text, View } from 'react-native'
import Child from './Child'

export default class Parent extends Component {
    render() {
        return (
            <View>
                <Text> Hi I am a parent component </Text>
                <Child/>
            </View>
        )
    }
}

Child.js

import React, { Component } from 'react'
import { Text, View } from 'react-native'

export default class Child extends Component {
    render() {
        return (
            <View>
                <Text> This is child </Text>
            </View>
        )
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-14
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 2016-11-05
    相关资源
    最近更新 更多