首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将React Hooks与react-native-tab-view一起使用

如何将React Hooks与react-native-tab-view一起使用
EN

Stack Overflow用户
提问于 2019-03-19 04:14:55
回答 2查看 1.5K关注 0票数 3

red error screen 1

Cannot read property 'configureProps' of undefined

我正在使用react-tab-view和react Hooks和typescript,但我有一些问题,有人可以帮我……

代码语言:javascript
复制
import React, { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet, Dimensions } from 'reactnative';
import { TabView, SceneMap } from 'react-native-tab-view';



const FirstRoute = () => (
    <View style={[styles.scene, { backgroundColor: '#ff4081' }]} />
);
const SecondRoute = () => (
    <View style={[styles.scene, { backgroundColor: '#673ab7' }]} />
);

interface Props {
    navigation: any;

}
export default function Home(props: Props) {


    const [rar, setRar] = useState({
        index: 0,
        routes: [
            { key: 'first', title: 'First' },
            { key: 'second', title: 'Second' },
        ]
    });

    var NativeAppEventEmitter = require('RCTNativeAppEventEmitter');
    return (
        <View>
            <TouchableOpacity onPress={props.navigation.openDrawer}>
                <Text>hola desde home</Text>
            </TouchableOpacity>

            <TabView
                navigationState={rar}
                renderScene={SceneMap({
                    first: FirstRoute,
                    second: SecondRoute,
                })}
                onIndexChange={index => ({ index })}
                initialLayout={{ width: Dimensions.get('window').width, height: 250 }}
            />


        </View>
    )
}
const styles = StyleSheet.create({
    scene: {
        flex: 0.3,
    },
});
EN

回答 2

Stack Overflow用户

发布于 2019-08-07 12:05:43

变化

代码语言:javascript
复制
onIndexChange={index => ({ index })}

代码语言:javascript
复制
onIndexChange={index => setRar({ ...rar, index })}

这应该可以修复您所面临的错误

票数 1
EN

Stack Overflow用户

发布于 2019-07-28 01:55:12

代码语言:javascript
复制
import React, { useState, useEffect } from 'react';
import { Container } from './styles';
import { View, StyleSheet, Dimensions } from 'react-native';
import { TabView, SceneMap } from 'react-native-tab-view';

const LatestRoute = () => (
  <View style={[styles.scene, { backgroundColor: '#ff4081' }]} />
);

const FavoritesRoute = () => (
  <View style={[styles.scene, { backgroundColor: '#673ab7' }]} />
);

const AllRoute = () => (
  <View style={[styles.scene, { backgroundColor: '#673ab7' }]} />
);

const styles = StyleSheet.create({
  scene: {
    flex: 1,
  },
});
export default function Main() {

  const initialState = {
    index: 0,
    routes: [
      { key: 'latest', title: 'Latest' },
      { key: 'favorites', title: 'Favorites' },
      { key: 'all', title: 'All' },
    ],
  };

  const [ state, setState ] = useState(initialState);

  function selectTab ( index ) {
    this.initialState = {
      index: index,
      routes: [
        { key: 'latest', title: 'Latest' },
        { key: 'favorites', title: 'Favorites' },
        { key: 'all', title: 'All' },
      ],
    };
    return setState(this.initialState);
  }
  
  return (
    <Container>
      <TabView
        navigationState={state}
        renderScene={SceneMap({  latest: LatestRoute,  favorites: FavoritesRoute, all: AllRoute  })}
        onIndexChange={ (index) => selectTab(index) }
        initialLayout={{ width: Dimensions.get('window').width, height : Dimensions.get('window').height }}
      />
    </Container>
  );
}

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55229373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档