Skip to content

ColumnWrapperStyle example #804

Closed Answered by BalogunofAfrica
BalogunofAfrica asked this question in Q&A
Discussion options

You must be logged in to vote

Solved this by creating a helper component and calculating the alignment of each column based on the index

import { StyleSheet, View, ViewProps } from "react-native";

const getItemStyle = (index: number, numColumns: number) => {
  const alignItems = (() => {
    if (numColumns < 2 || index % numColumns === 0) return "flex-start";
    if ((index + 1) % numColumns === 0) return "flex-end";

    return "center";
  })();

  return {
    alignItems,
    width: "100%",
  } as const;
};

type ColumnItemProps = ViewProps & {
  children: React.ReactNode;
  index: number;
  numColumns: number;
};
export const ColumnItem = ({ children, index, numColumns, ...rest }: ColumnItemProps) => (
    <View s…

Replies: 4 comments 7 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@BalogunofAfrica
Comment options

@hirbod
Comment options

@BalogunofAfrica
Comment options

@hirbod
Comment options

@BalogunofAfrica
Comment options

Answer selected by BalogunofAfrica
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@seva3l
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants