【问题标题】:How to set the title of the scene that's a child of a tab using react-native-router-flux如何使用 react-native-router-flux 设置作为选项卡子级的场景的标题
【发布时间】:2019-01-04 15:59:04
【问题描述】:

我有这条路线:

    export default props => (
        <Router>
            <Stack key="root" hideNavBar>
                <Tabs key='tabs'
                      tabs={true}
                      initial
                      tabBarStyle={{backgroundColor: THEME_NAVBAR_AND_TABBAR}}
                      showLabel={false}>
                    <Scene key='events' component={Events} title='Events' icon={TabIcon}/>

                </Tabs>
            </Stack>
        </Router>
    )

在我的事件组件上,我有这个:

 import React from 'react'
 import {View, Text, ScrollView, TouchableWithoutFeedback, 
 TouchableOpacity} from 'react-native'
 import {connect} from "react-redux"

 import style from './style'
 import {Actions} from "react-native-router-flux";

class Events extends React.Component {
constructor(props) {
    super(props)
    this.state = {
        loaded: false
    }
}
componentDidMount() {
    Actions.refresh({ title: 'xxxx' })
}
render() {
    return (
        <ScrollView style={style.scene}>

        </ScrollView>
    )
}
}

const mapStateToProps = state => (
{
    events: state.ContentReducer.events
}
)

export default connect(mapStateToProps,
{

})(Events)

但它被忽略了。可能是因为场景是 Tabs 的子节点。如何做出这种改变? (它必须是我在组件内 - 不能在路由器上)

【问题讨论】:

    标签: react-native react-native-router-flux


    【解决方案1】:

    经过多次研究和反复试验,我找到了解决方案:

    this.props.navigation.setParams({
                title: 'xxxx'
            })
    

    【讨论】:

      【解决方案2】:

      这是因为选项卡默认加载所有嵌套的场景,您可以通过“懒惰”的道具来防止这种情况。但是如果你打开场景一次,你将需要使用 componentWillReceiveProps 来获取新的道具,因为即使你改变标签,场景也会保持打开状态。

      在Events组件场景中,你可以使用标题作为状态,然后使用componentWillMount函数获取标题,使用componentWillReceiveProps函数在标题发生变化时获取新标题。

      componentWillMount() {
        this.setState({title: this.props.title});
      }
      
      componentWillReceiveProps(newProps) {
        //new title
        this.setState({title: newProps.title});
      }
      

      【讨论】:

      • 谢谢,亚历山德罗!这是有道理的。但是你能告诉我我应该在路由器上做什么来存档它吗?请编辑您的答案。谢谢!
      • @MarcoJr 我可以发布事件组件吗?我可以帮你更多。
      • 当然可以!我刚刚为你编辑了代码。但无论如何,这是一个非常赤裸裸的场景。如果您需要更多,请告诉我!
      • @MarcoJr 好的,首先对不起,我想念你想做什么,但是要更改 TABBAR 标题,你可以使用 Actions.Events({title: "Novo Titulo"}) 或者你可以通过Actions.refresh({key: "Events", title: "Novo Titulo" }) 上的键。不要与屏幕顶部显示的 NAVBAR 标题混淆。
      • 看起来你和我一样是个 pt-BR :) 嗯....不,不走运...我之前读过这个解决方案并尝试过:Actions.refresh({ key: 'events', title: 'xxxx' }) Actions.events({title : 'yyyy'}) 上面的行都没有解决它......这很令人沮丧,但我确实感谢你的努力。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2017-01-05
      • 2018-02-21
      相关资源
      最近更新 更多