react-native - 当 redux 状态改变时改变 tabBarIcon
问题描述
我在下面定义navigationOptions
了App.js
这样的流程:
应用程序.js
const intimationsFlow = createStackNavigator({
Feed: FeedContainer,
Edit: EditContainer
});
intimationsFlow.navigationOptions = ({ navigation }) => {
let tabBarVisible = true;
if (navigation.state.index > 0)
tabBarVisible = false;
return {
title: '',
tabBarIcon: ({ focused }) => {
const { pushNotificationSeen } = store.getState();
console.log('pushNotificationSeen', pushNotificationSeen);
let i;
if(pushNotificationSeen) {
if (focused) {
i = <FontAwesomeIcon icon={'bell'} size={29} color={'#3780BE'} />
} else {
i = <FontAwesomeIcon icon={'bell'} size={29} color={'#393939'} />
}
} else {
if (focused) {
updatePushNotificationSeen(true);
i = <FontAwesomeIcon icon={'bell'} size={29} color={'#3780BE'} />
} else {
i = <><FontAwesomeIcon icon={'bell'} size={29} color={'#393939'} /><Badge status="error" badgeStyle={{ position: 'absolute', top: -26, right: -13 }} /></>
}
}
return i;
},
tabBarVisible
};
};
const AppNavigator = createSwitchNavigator({
ResolveAuth: ResolveAuthScreen,
mainFlow
});
const App = createAppContainer(AppNavigator);
export default () => {
return <Provider store={store}>
<SafeAreaProvider>
<App ref={navigatorRef => { setTopLevelNavigator(navigatorRef) }} />
</SafeAreaProvider>
</Provider>
};
我想根据是否已经看到推送通知来更新 tabBarIcon 。如果尚未看到推送通知,那么我会显示一个徽章。
这里的问题是我只能在标签栏上有活动时才能获取状态。但我想要的是,每当pushNotificationSeen
更新状态时,应该重新渲染 tarBarIcon。
请建议是否可能,否则如何实现。谢谢。
解决方案
您必须在 componentWillReceiveProps 处收听减速器中的推送通知更改
class YourComponent extends React.Component {
{...}
static navigationOptions = {
title: ({ state }) => `there is ${state.params && state.params.pushNotificationSeen ? state.params.pushNotificationSeen : ''}`,
{...}
}
{...}
componentWillReceiveProps(nextProps) {
if (nextProps.pushNotificationSeen) {
this.props.navigation.setParams({ pushNotificationSeen });
}
}
}
const connectedSuperMan = connect(state => ({ pushNotificationSeen: state.ReducerYo.pushNotificationSeen }))(YourComponent);
推荐阅读
- visual-studio-code - vscode 运行语言时做自定义命令
- regex - JQ - 如何为特殊字符定义正则表达式
- swift - 我想弄清楚下面代码中的“A”应该是什么
- php - 在 php 中解析嵌套的 Json 数组
- javascript - 你可以使用一个对象字面量作为另一个对象字面量的键吗?这样做有什么影响?
- thunderbird - 写入当前邮件的邮件地址
- javascript - 如何用文本替换播放/暂停图标切换
- mysql - MySql 计数表值以逗号分隔
- html - 使用 css 选择器 excel vba 从网站抓取数据
- javascript - 完整日历需要帮助 - 尝试做每周数据