【问题标题】:How can I trigger a tabItem onPress within a component using React Native?如何使用 React Native 在组件中触发 tabItem onPress?
【发布时间】:2015-11-23 09:27:50
【问题描述】:

好吧,我放弃了。这一定很基础,没有人在任何地方记录它。

我有 3 个基本组件,所有组件都可以从具有 3 个选项卡项的基本组件中单击。

如何模拟用户单击其中一个选项卡项?例如,假装 component1 有一个动作 - 当成功时 - 将用户引导到 component2(就像他们从 tabitem 中单击它一样)

我可以提供一些代码,否则假设我有一个基本的教程应用程序。我觉得我正在寻找的本质上是一个超链接。

【问题讨论】:

  • 当动作触发时你是否试图导航到另一个组件?当你调用一个 onPress 函数时,你基本上将另一个函数与你的功能一起传递给它,所以我假设你可以忽略 onPress 并在你想要发生的任何模拟之后直接调用该函数。
  • 谢谢@NaderDabit 是的,我正在寻找那个。我觉得自己像个白痴,但我不知道如何“在您想要发生的任何仿真之后直接调用该函数”。我已经尝试过 Component2.render() 等。似乎没有任何效果。

标签: react-native


【解决方案1】:

您可能应该将 React 组件视为一棵树,并且您可以将要触发的函数从父级传递给子级作为道具。

假设你有一个 Tab 组件 -

const Tab = React.createClass({

  onPressButton(){
    triggeredFunction();
  },

  render() {
    return (
      <Button onPress={this.onPressButton}>
        <Text>Press Me!</Text>
      </Button>
    );
  },

});

如果你想模拟触摸,你可以简单地调用triggeredFunction()(或在组件中this.onPressButton)。

如果您尝试从父组件触发函数,您可能应该在父组件中拥有触发函数并将其作为 prop 传递 -

const Tab = React.createClass({

propTypes: {
  onPressButton: React.PropTypes.func,
  tabNumber: React.PropTypes.number,
  tabText: React.PropTypes.string,
},

triggerTheTriggerToTrigger() {
  // This will trigger the triggeredFunction in the page component and pass in the tab number
  // Remember that onPressButton={this.triggeredFunction}
  // So you are calling this.triggeredFunction(tabNumber) in the parent page component
  this.props.onPressButton(this.props.tabNumber);
},

  render() {
    return (
      <Button onPress={this.triggerTheTriggerToTrigger}>
        <Text>{this.props.tabText}</Text>
      </Button>
    );
  },

});

然后在你的主要组件中

const Page = React.createClass({

getInitialState() {
 return {
  currentTab: 1,
 };
},

triggeredFunction(tabNum) {
 // This function is setting the state of the page component to be equal to that passed from the tab
 // So when the tab is touched it will trigger the page to change to that number.
 this.setState({
  currentTab: tabNum,
 });
},

// main component render:
  render() {

   let content;

   // We are setting the page 'content' from here
   // Choosing the content from the currentTab state
   switch (this.state.currentTab) {
    case 1:
     content = <Text>This is the content for tab 1</Text>
    break
    case 2:
     content = <Text>Tab 2 has a slightly different message</Text>
    break
    case 3:
     content = <Text>Tab 3 is slightly different too</Text>
    break
   }

    return (
    <View className="page">
     <View className="toptabs">
      <Tab onPressButton={this.triggeredFunction} tabText="Button 1" tabNumber={1} />
      <Tab onPressButton={this.triggeredFunction} tabText="Button 2" tabNumber={2} />
      <Tab onPressButton={this.triggeredFunction} tabText="Button 3" tabNumber={3} />
     </View>
     <View className="pageContent">
      {content}
     </View>
    </View>
    );
 },
});

然后您可以改为从主组件调用this.triggeredFunction()。我希望这是有道理的。

我还没有测试过这段代码,所以它可能需要一些调整,但希望它能向你展示它背后的逻辑。

另外,我在这里使用了一个 switch 语句来使正在发生的事情变得明显。我可能不会在真正的应用程序中使用这种方法(并不是说它特别糟糕)。您还可以加载其他组件并根据 currentTab 状态有条件地加载它们。您可以创建一个内容数组并拥有 - let content = contents[this.state.currentTab];

您也可以通过其他方式实现此目的。我在我的应用程序中使用了通量,其中包含您更新的商店。这些存储然后用数据传播视图。这意味着您可以获得更多的全局设置。如果您使用的是flux,那么您基本上可以从flux中设置页面状态(即tabNumber)。

因此,在您的标签中,您可以将 onPressButton 设置为 - this.flux.action.updateTab(this.props.tabNumber); 这将更新全局存储以设置您所在的 tabNumber(即您不再只是在页面组件上设置 currentTab)

在您的页面中,您可以通过以下方式获取 currentTab 状态 - currentTab: this.flux.store.tabStore.getCurrentTab() 所以商店会在更新时更新您的页面组件。

但这是一个比您使用的更复杂的实现,它超出了我们在这里讨论的范围。如果该部分令人困惑,请不要担心,但如果您将来要构建更大的东西,考虑它可能会有所帮助(想象一下 10 个不同的应用程序“页面”,其中包含用于不同事物的选项卡部分,突然间您想要一个存储空间可以控制它们的状态,而不是在每组组件中)。

【讨论】:

  • 这是有道理的,但我仍然没有看到如何实际呈现组件。我看到这是如何对函数执行 onPress,但实际上将显示切换到另一个组件的 TriggeredFunction() 应该是什么。
  • 我有点噩梦试图在这个评论框中写一个完整的回复,所以我会添加到我的解决方案中。
  • 好的,我更新了上面的解决方案,使它包含一个完整的实现,你将如何在父页面上编写标签并触发一个函数。如果您觉得有帮助,请给我 +1。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
  • 1970-01-01
  • 2018-02-27
  • 2021-03-28
相关资源
最近更新 更多