我想添加重新加载'按钮到我的标题,我通过调用setParams来做。问题是这会导致TabNavigator导航到启动时的选项卡
export default class FeedScreen extends React.Component {
static navigationOptions = ({ navigation }) => {
const { params } = navigation.state;
if (params) {
return {
headerRight: <Icon.Button
color="black"
backgroundColor="transparent"
underlayColor="transparent"
name="reload"
onPress={ params.reload }
/>
};
}
}
constructor (props) {
super(props);
// We want to set attach a reload function to the navigation
this.props.navigation.setParams({
reload: this._reloadFeed.bind(this)
});
}
那么有没有一种方法setParams无法导航到这个场景,或者有没有办法将函数分配给这个图标而不调用setParams?
答案 0 :(得分:0)
您是否尝试将onPress = {params.reload}设置为onPress = {()=&gt; params.reload()}
export default class FeedScreen extends React.Component {
static navigationOptions = ({ navigation }) => {
const { params } = navigation.state;
if (params) {
return {
headerRight: <Icon.Button
color="black"
backgroundColor="transparent"
underlayColor="transparent"
name="reload"
onPress={ () => params.reload() } < -----
/>
};
}
}
constructor (props) {
super(props);
// We want to set attach a reload function to the navigation
this.props.navigation.setParams({
reload: this._reloadFeed.bind(this)
});
}
答案 1 :(得分:0)
您可能正在寻找Navigation Actions,特别是名为“setParams”的导航操作(请勿与navigation.setParams()
混淆),您可以通过以下方式发送:navigation.dispatch(myCreatedSetParamsAction)
答案 2 :(得分:0)
我能够通过从头部调用静态函数来解决这个问题,并将该静态函数更新到componentDidMount中的重载函数。它很酷,但很有效。
我的组件外的功能:
let _reloadFunction = () => {
// This exists because react navigation is a mess
}
导航选项
static navigationOptions = {
headerRight: <Icon.Button
color="black"
backgroundColor="transparent"
underlayColor="transparent"
name="reload"
onPress={() => { _reloadFunction() }}
/>
}
在组件中:
// Gonna update that reloadFunction so the header works
_reloadFunction = this._reloadFeed.bind(this);