在React-native中更改一个组件的状态与另一个组件的状态

时间:2017-12-28 08:55:49

标签: javascript reactjs react-native redux react-navigation

我有以下代码,我希望实现的是从Tabscreen中的某个屏幕更改TabNavigator的状态。但是我正在尝试这样做的方式,使用全局函数设置状态,根本不起作用,错误undefined is not a function (evaluating 'this.setstate')

import React from 'react';
import {Image, Text, TouchableNativeFeedback, TouchableHighlight} from 'react-native';
import {TabNavigator} from 'react-navigation';
import {Container, Header, Icon, Left, Body} from 'native-base';
import Timeline from './Timeline';

const Mainscreen=TabNavigator(
  {
      Main:{
        screen: Timeline,
        navigationOptions:{
          tabBarIcon: ({tintColor}) => <Icon name='home' style={{color:tintColor}}/>
        }
      },
      Search:{
        screen: props => <Container><TouchableHighlight onPress={()=>globalStateUpdate()}><Container style={{backgroundColor:'rgba(0,132,180,0.5)'}}></Container></TouchableHighlight></Container>,
        navigationOptions:{
          tabBarIcon: ({tintColor}) => <Icon name='search' style={{color:tintColor}}/>
        }
      },
  }
);

function globalStateUpdate(){
  this.setState({
    header:<Text>Search</Text>
  });
}

class Tabscreen extends React.Component {
  constructor(props){
    super(props);
    this.state={
      header:<Text>Home</Text>
    };
    globalStateUpdate = globalStateUpdate.bind(this);
  }

  render() {
    return (
      <Container>
        <Header hasTabs >
          <Left>
            {this.state.header}
          </Left>
          <Body/>
        </Header>
        <Mainscreen/>
      </Container>
    );
  }
}

export default Tabscreen;

我该怎么做?反应导航对我的应用程序非常重要,我无法删除它。我相信Redux可以解决这个问题,但是对于单个文本更改来说似乎过于复杂,这就是我所需要的。

1 个答案:

答案 0 :(得分:1)

您可以将此功能从TabScreen传递到其他屏幕以更新TabScreen状态。

const Mainscreen=TabNavigator(
    {
        Main:{
            screen: Timeline,
            navigationOptions:{
                tabBarIcon: ({tintColor}) => <Icon name='home' style={{color:tintColor}}/>
            }
        },
        Search:{
            screen: props => <Container><TouchableHighlight onPress={()=> props.screenProps.globalStateUpdate({header:<Text>Search</Text>})}><Container style={{backgroundColor:'rgba(0,132,180,0.5)'}}></Container></TouchableHighlight></Container>,
            navigationOptions:{
                tabBarIcon: ({tintColor}) => <Icon name='search' style={{color:tintColor}}/>
            }
        },
    }
);


class Tabscreen extends React.Component {
    constructor(props){
        super(props);
        this.state={
            header:<Text>Home</Text>
            globalStateUpdate: newState => this.setState(newState)
        };
    }

    render() {
        return (
            <Container>
                <Header hasTabs >
                    <Left>
                        {this.state.header}
                    </Left>
                    <Body/>
                </Header>
                <Mainscreen screenProps={{globalStateUpdate: this.state.globalStateUpdate}} />
            </Container>
        );
    }
}