javascript - 如何在React Native中调用带有来自另一个类的参数的函数?

标签 javascript reactjs react-native

我想在一个屏幕中显示 Side_Drawer 上列出的不同屏幕。我不想为每个屏幕创建相同的布局更改,因此当用户单击列表时,屏幕的中心部分会相应更改,而其他布局将保持不变。

这是我的屏幕 A,其中内容随着状态中索引的变化而变化。

   class ScreenA extends Component {
      constructor(props) {
        super(props)
        this.state = { index: 0 }; // default screen index
      }
          switchScreen(index) {
            this.setState({ index: index })
          }
      render() {
        let AppScreen = null;

        if (this.state.index == 0) {
          AppScreen = Screen1
        } else if (this.state.index == 1) {
          AppScreen = Screen2
        } else if (this.state.index == 2) {
          AppScreen = ScreenC        }

        return (
          <Drawer ref={(ref) => { this.drawer = ref; }}
            content={<Side_Drawer navigator={this.navigator} />}
            onClose={() => this.closeDrawer()} >
             <Container>    
             <Button
                onPress={() => this.switchScreen(1)}/>

<AppScreen />
             <Containert/>        
            </Drawer>  
        );
      }
    }

这是我的 Side_Drawer,它是 ScreenA 抽屉上的列表,我想从中更改屏幕 A 的 switchScreen 函数的索引和我想要显示屏幕 C 的列表的 onPress。

class Side_Drawer extends Component {
  render() {
    return (
      <Container>
        <Content>
            <ListItem  >
              <Text onPress={()=>this.props.navigation.navigate('ScreenC')}>Goto Screen C</Text>
            </ListItem>
        </Content>
      </Container>
    );
  }
}

module.exports = withNavigation(Side_Drawer);

我想要一个单屏应用程序,其中只有屏幕所需的部分会发生变化,其他部分都相同。

最佳答案

传递要使用的参数

您可以将其移至侧面菜单,因为您有屏幕数据要移至 AppScreen。

content={<Side_Drawer navigator={this.navigator} move={AppScreen} />}

Side_Drawer强文字

<Text onPress={()=>this.props.navigation.navigate(this.props.move)}>

关于javascript - 如何在React Native中调用带有来自另一个类的参数的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57622978/

相关文章:

javascript - 如何在 promise 的 componentDidMount 中设置状态而不会出现 setState 错误

reactjs - 需要帮助才能使 [create-react-app] 使用异步 - 等待(将异步转换为生成器)!

react-native - React Native - 如何自动将 ScrollView 滚动到光标位置?

javascript - 将变量传递到符合 W3C 的外部 js 文件中

javascript - 将 object.keys 从对象数组映射到字符串

javascript - React Router with Private Route 呈现空白页面

android - react-native run-android 后的两个app

javascript - 如何过滤掉来自多个状态的数据,在React Native中从单独的API获取数据

javascript - JS : how to shift each letter in the given string N places down in the alphabet?

javascript - Protractor :如何在一个规范执行结束后保持浏览器实例运行