reactjs - 如何在渲染另一个组件后渲染一个 React Native 组件?

标签 reactjs react-native

假设我有 2 个组件 AB。我想渲染组件 A,然后渲染组件 B。为了使这一点更加明显,我还想在 AB 的渲染之间设置一些延迟(假设 2 秒)。解决这个问题的最佳方法是什么?我想我应该以某种方式触发 AcomponentDidMountB 的渲染,但我真的不知道如何触发。

谢谢:)

最佳答案

您的问题非常模糊并且可以多种实现。 这是我的看法:

export default class App extends Component {
  constructor() {
    super()
    this.state = { displayComponentB: false }
    this.displayComponentB = this.displayComponentB.bind(this)
  }
  displayComponentB() {
    setTimeout(() => {
      this.setState({ displayComponentB: true })
    }, 2000) // delay
  }
  render() {
    return (
      <View style={styles.container}>
        <ComponentA onComponentDidMount={this.displayComponentB}/>
        {
          this.state.displayComponentB && 
          <ComponentB />
        }
      </View>
    );
  }
}

export class ComponentA extends Component {
  componentDidMount() {
    this.props.onComponentDidMount && this.props.onComponentDidMount()
  }
  render() {
    return (
      <View style={[styles.component, styles.componentA]}>
        <Text style={styles.text}>Component A</Text>
      </View>
    );
  }
}

完整代码和现场演示:https://snack.expo.io/SkIOLJ3eM

关于reactjs - 如何在渲染另一个组件后渲染一个 React Native 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47547465/

相关文章:

javascript - 将 LocalStorage 与 React 结合使用?

reactjs - 单击事件后清除输入文本

ios - 如何在 React Native 中支持 WebP 图片?

react-native - 有没有办法更改 IOS 标题上的 react 导航默认后退按钮颜色?

javascript - ReactJs - 如何将 ApolloProvider 包装在 StatsigProvider 中?

javascript - 使用 CSS 悬停伪选择器时,组件无法在 React 中正确渲染

javascript - React Native 怪异的 flex 行为

reactjs - 在 Windows 上,react-native init 不适用于 React-native 0.57.1

javascript - 用大对象响应更新状态

react-native - react-native\react.gradle'不存在