-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider renaming initialPage
#126
Comments
absolutely agree |
It depends on expected by user behaviour, we can either:
|
First option is better for me. |
So what was the conclusion here - how can this problem of popping back to the initial set page be solved? |
@robtg4 i think it's not the component job to do so it's the user implementation to decide the initial state using redux or what ever he wants |
UPDATE 2 - solution UPDATE All of i wrote next will work only for controlled tabs with Redux, but i catch scroll back if just swipe to other tab (if change with Redux or tap on TabBar all work fine). TL;DR I use redux for save my current tab (I change it in other place). constructor() {
super();
this.pageProps = {} // can declare just in class
this.pageIndex = null // ** Update **
} Ok we have object for save component`s page data; handleChangeScreen = ({ i }) => {
this.pageIndex = i; // **Update ** save current page index
this.props.dispatch(setMainScreen(i));
} just handle onChangeTab and set in Redux ( if (!this.pageProps.initialPage) this.pageProps.initialPage = mainScreen;
if (!this.pageProps.page || !this.pageProps.page !== mainScreen) this.pageProps.page = mainScreen;
Update componentWillReceiveProps(nextProps) {
const { YouStore: { mainScreen } } = nextProps;
if ( this.props.YouStore.mainScreen !== mainScreen && mainScreen !== this.pageIndex) {
if (this.tabView) this.tabView.goToPage(mainScreen)
}
} <ScrollableTabView
{...this.pageProps}
onChangeTab={this.handleChangeScreen}
ref={(tabView) => { this.tabView = tabView; }} // set tabView
...
/> Add props to I try explain in detail, perhaps i can`t did it =). |
i think many components has an issue with interacting with redux the main reason is sometime you need redux to control the component and sometime you need the component to control redux maybe all components should consider doing the following scenario:
|
in this way you can listen to the event cancel it and change your redux state in the other hand redux will change your component prop again :) |
@digital-flowers yeah, i agree, that will be awesome. If we don`t control component - just do u job, but if we do - doesn`t disturb =) |
exactly ! |
UPDATE Sorry for long read |
@dictory Could you elaborate how you would use |
@Froelund, my post with update actual now(doesn't content old code).
handleChangeScreen = () => {
this.pageIndex = i; // save local too
this.props.dispatch(saveCurrentTabToRedux(i));
}
...
<ScrollableTabView
...
onChangeTab={this.handleChangeScreen}
>
this.props.dispatch(saveCurrentTabToRedux(2)); Next. Add <ScrollableTabView
...
ref={(tabView) => { this.tabView = tabView; }}
> Detect page change outside: componentWillReceiveProps(nextProps) {
const { YouStore: { pageIndex } } = nextProps;
if ( this.props.YouStore.mainScreen !== pageIndex && pageIndex !== this.pageIndex) {
if (this.tabView) this.tabView.goToPage(pageIndex)
}
} You are awesome. Don`t forget set initialPage. |
@dictory : @grabbou , @digital-flowers : So I ask is there a way to know index of the tab that is selected in every moment, how can I do, I do not know using its reference by chance? |
@dictory Just one thing, in this section:
the variable "i" is not mentioned where it comes from, should be
|
in @dictory example instead of using the local variable |
@dictory I am finding it hard to understand your instructions. Can you please provide an Expo snack for this? |
@khat33b sorry, I stoped use this library in my projects. |
@eseQ , what library do you use now? could you share with me? thanks |
@newCaoTao, @khat33b last year I work on class MainScreen extends React.Component {
tabView = null;
loadCount = 0;
screenKey = null;
initialPage = null;
screens = [
{
key : 'tags',
component: () => <TagsListAll key="tags" tabLabel="#" />,
},
...
]
componentWillReceiveProps(nextProps) {
// mainScreen from store
const { mainScreen } = nextProps;
if (
this.props.mainScreen !== mainScreen && mainScreen !== this.screenKey &&
this.loadCount > 2 && this.screenKey !== null && !!this.tabView
) this.tabView.goToPage(this.getScreenIndex(nextProps));
}
getScreenIndex = (props = this.props) => findIndex(this.screens, ['key', props.mainScreen]);
renderTabBar = () => (
<ScrollableTabBar
style={styles.tabBar}
tabsContainerStyle={styles.tabsContainer}
tabStyle={styles.tab}
/>
)
handleChangeScreen = ({ i }) => {
this.screenKey = (this.screens[i] && this.screens[i].key) || (this.otherPosts[i] && this.otherPosts[i].key);
this.props.dispatch(setMainScreen(this.screenKey));
}
getList = list => map(list, screen => screen.component({ settings: this.props.Settings }))
render() {
const { mainScreen } = this.props;
const screens = this.getList(this.screens.concat(this.otherPosts));
this.loadCount += 1; // hook strange bug
if (this.initialPage === null) {
const screenIndex = this.getScreenIndex();
this.screenKey = mainScreen;
this.initialPage = screenIndex;
}
return (
<ScrollableTabView
initialPage={this.initialPage}
onChangeTab={this.handleChangeScreen}
renderTabBar={this.renderTabBar}
ref={(tabView) => { this.tabView = tabView; }}
...
>
{screens}
</ScrollableTabView>
);
}
}
export default MainScreen; I recommend use functional component and |
having trouble with the StateLess component. |
It's actually
currentPage
since it reacts to changes https://github.com/brentvatne/react-native-scrollable-tab-view/blob/master/index.js#L55 (or maybe at least clarify that in docs?)The text was updated successfully, but these errors were encountered: