在React Native中的两个组件之间传递数据

阿卜杜勒·埃拉·JS

我有一个封装了三个组件的组件,其中一个是DrawerLayoutAndroid现在,我想将抽屉的引用从抽屉组件传递到主组件,然后将其传递到标题组件,以便可以从标题组件触发它。

我不知道该怎么做。

这是主要组件“ cases.js”

import React, { Component } from 'react';
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
import Header from '@components/header';
import BottomBar from '@components/bottom-bar';
import SideBar from '@components/drawer';

export default class Cases extends Component {

  state = {
    title : 'Cases'
  }

  change_text = () => {

    this.setState({ title : 'Test' })

  }

  open_drawer = (ref) => {

    ref.openDrawer();

  }

  close_drawer = (ref) => {

    ref.closeDrawer();

  }

  render() {

    return (

      <SideBar style={ styles.container }>

        <Header title={ this.state.title } change_text={ this.change_text } open={ this.state.side_bar } />

        <View style={ styles.body }>

          <Text style={ styles.text }> { this.state.name } </Text>

          <TouchableOpacity onPress={ () => this.change_text() } style={ styles.button }>

            <Text> Change State </Text>

          </TouchableOpacity>

        </View>

        <BottomBar ref={ this.state.side_bar } />

      </SideBar>

    )

  }

}

const styles = StyleSheet.create({
  container : {
    flex : 1,
    flexDirection: 'column',
  },
  body : {
    flex : 1,
    backgroundColor: '#ccc',
    justifyContent: 'center',
    alignItems: 'center'
  },
  text : {
    color : '#fff'
  },
  button : {
    backgroundColor : '#eee',
    paddingVertical: 5,
    paddingHorizontal: 10
  }
})

这是我的标题:

import React, { Component } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, Image } from 'react-native';
import DrawerLayoutAndroid from 'DrawerLayoutAndroid';
import LinearGradient from 'react-native-linear-gradient';

export default class Header extends Component {

  change_text = () => {

    this.props.change_text();

  }

  open = ()

  render () {

    return (

      <LinearGradient start={{x: 0, y: 0}} end={{x: 1, y: 0}} colors={['#a4d294', '#3ac6f3']} style={ styles.header }>

          <TouchableOpacity onPress={ () => this.props.change_text() }>

            <Image source={ require('@media/images/add.png') } style={ styles.add_button } />

          </TouchableOpacity>

          <Text style={ styles.title }> { this.props.title } </Text>

          <TouchableOpacity onPress={ () => this.props.open() }>

            <Image source={ require('@media/images/more.png') } style={ styles.more_button } />

          </TouchableOpacity>

      </LinearGradient>

    )

  }

}

const styles = StyleSheet.create({
  header : {
    height: 70,
    backgroundColor: '#eee',
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center'
  },
  title : {
    color : '#fff',
    textAlign: 'center',
    fontSize: 20,
    letterSpacing: 3
  },
  add_button : {
    width: 30,
    height: 30,
    marginHorizontal: 10
  },
  more_button : {
    width: 30,
    height: 30,
    marginHorizontal: 10
  }
})

这是我的drawer.js

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import DrawerLayoutAndroid from 'DrawerLayoutAndroid';
import LinearGradient from 'react-native-linear-gradient';

export default class Drawer extends Component {

  render () {

    const NavigationMenu = (
      <LinearGradient start={{x: 0, y: 0}} end={{x: 0, y: 1}} colors={['#a4d294', '#3ac6f3']} style={{ flex: 1 }}>
        <View style={{ flex : 6, justifyContent: 'center' }}>
          <Text>He There</Text>
        </View>
      </LinearGradient>
    )

    return (

      <DrawerLayoutAndroid
        drawerWidth={250}
        drawerPosition={ DrawerLayoutAndroid.positions.Right }
        renderNavigationView={ () => NavigationMenu }
        ref={ (drawer) => this.props.ref = drawer }
      >

        { this.props.children }

      </DrawerLayoutAndroid>

    )

  }

}
昌迪尼

我将通过一个简单的例子来回答您的问题。让我们以父级和子级两个部分为例。您希望在父级中将某些消息传递给父级,并在子级中发生事件时从父级中获得回调。

export default class Parent extends React.Component<any, any> {

  callback (paramFromChild) => {
    // implement what to do when certain event occurs in child component
  }

  render () {
    <View>
       <Child message={"some text"} callbackFromChild={this.callback}/>
    </View>
  }
}

子组件

Interface childProps {
   message: string

   callbackFromChild(paramFromChild);
}

export default class Child extends React.Component<childProps, any> {

  render () {
    <View>
       <Button title={this.props.message} onPress={this.props.callbackFromChild("some message from child")}/>
    </View>
  }
}

这样,您可以使用道具在不同组件之间进行通信。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在React Native Navigator中在组件之间传递值

在两个同级React.js组件之间传递数据

在React Native中的两个单独的应用程序之间共享代码

计算React Native中两个经纬度坐标之间的距离?

如何在React Native中显示来自两个不同键值的过滤后的JSON数据

在两个React应用之间共享组件

React组件中的两个提供者

在React中的两个组件之间切换

在两个堆栈之间传递参数-React Native

如何将mapStateToProps链接到同一React Native组件中的两个类

如何在React-Native中解决模态内两个组件之间的边距问题?

计算React Native中两个日期之间的差异

如何在React Native中显示来自两个Firestore集合的数据

在React组件之间传递数据

React Selector:通过两个组件传递onChange事件

如何在React的3个组件之间传递数据?

如何找到React Native的两个元素之间的距离?

如何在React中的同级组件之间传递数据?

在react组件中调用两个API调用

如何在 React Native 中对齐两个组件

在 REACT 中的两个函数之间传递信息

React-Native:在单个 View 组件中居中两个文本组件

如何在react js中的两个独立类之间连续传递数据?

在 ReactJS 中的两个独立组件之间传递数据

如何在react js中的两个子组件之间传输数据?

React 中两个子组件之间的传递函数

是否可以在 React 中的 2 个功能组件之间传递数据?

在 React 组件中组合两个 switch/case 语句

在 React Native 中的组件之间传递数据