简体   繁体   English

如何有条件地更改 React Native 中平面列表上的数据?

[英]How can I conditionally change data on a flat list in React Native?

in my app I have such scenario: when opening on first time I need to show all companies on screen, when user select a category I need to change the data of my FlatList to selected category companies.在我的应用程序中,我有这样的场景:第一次打开时,我需要在屏幕上显示所有公司,当用户 select 一个类别时,我需要将我的FlatList的数据更改为选定的类别公司。 I use conditional select data for the FlatList but this is not working.我对FlatList使用有条件的 select 数据,但这不起作用。 here is my code:这是我的代码:

const Home = (props) => {
  const dispatch = useDispatch();
  const companies = useSelector(state => state.companies.availableCompanies);
  const selectedCatCompanies = useSelector(state => state.selectedCatCompanies.availableSelectedCatCompanies)

  const loadCompanies = useCallback(async () => {
    await dispatch(CompaniesActions.fetchCompanies(1, 1, 1));
  }, [dispatch]);

  useEffect(() => {
    loadCompanies().then(() => {
      setIsLoading(false)
    })
  }, [dispatch, loadCompanies]);

  const renderCards = (itemData) => {
    return (
      <View style={styles.cardContainer}>
        <Card
          companyId={itemData.item.id}
          companyName={itemData.item.name}
          companyImage={itemData.item.image}
          companyMainAddress={itemData.item.mianAddress}
          companyPhoneNumber={itemData.item.telephoneNumber}
          companyDetails={itemData.item.details}
          onPress={() => { props.navigation.navigate('CardDetails')}}
        />
      </View>
    )
  }

  return (
    <SafeAreaView style={styles.screen}>
      <MainHeader />
      <SearchBar />
      <FlatList
        ListHeaderComponent={
          <>
            <Text style={styles.timeText}>Choose appropriate time:</Text>
            <View style={styles.timeContainer}>
              <PrimaryButton
                title='Choose a date'
                hasArrow={true}
                arrowSize={hp(2)}
              />
              <PrimaryButton
                title='Choose a time'
                hasArrow={true}
                arrowSize={hp(2)}
              />
            </View>
            <TypeSelection
              options={[
                { name: 'Premium', selected: false },
                { name: 'LifeStyle', selected: false },
                { name: 'Many Amenities', selected: false },
                { name: 'Gourment', selected: false },
              ]}
              selectedButtonStyles={styles.selectedButton}
              selectedButtonTextStyles={styles.selectedButtonText}
            />
          </>
        }
        nestedScrollEnabled={true}
        showsVerticalScrollIndicator={false}
        data={selectedCatCompanies == [] ? companies : selectedCatCompanies} //here is what I set 
        keyExtractor={item => item.id.toString()}
        renderItem={renderCards}
      />
    </SafeAreaView>
  )

and here is my screenshot for better understanding:这是我的屏幕截图,以便更好地理解:

在此处输入图像描述

I get my companies from 2 different APIs.我从 2 个不同的 API 获得我的公司。 with this code I only get my companies when user select a category but if non category selected it will not show me the result.使用此代码,我仅在用户 select 一个类别时才获得我的公司,但如果未选择类别,则不会显示结果。 hope I explain my question well.希望我能很好地解释我的问题。

I don't have your full code so fishing here.我没有你的完整代码,所以在这里钓鱼。 From your code guessing, you are using Redux.根据您的代码猜测,您使用的是 Redux。

Step 1) change selectedCatCompanies == [] to selectedCatCompanies.length === 0 [Thanks to Linda's input in the comment I modified this step 1]第 1 步)将selectedCatCompanies == []更改为selectedCatCompanies.length === 0 [感谢 Linda 在评论中的输入,我修改了这一步 1]

If step 1 does not work out then,如果步骤 1 不成功,那么,

Step 2) Try looking at CompaniesActions action creator and its corresponding reducer to confirm if you are handling both the use cases of companies and selectedCatCompanies.第 2 步)尝试查看 CompaniesActions 操作创建者及其对应的 reducer,以确认您是否同时处理公司和 selectedCatCompanies 的用例。 I think the issue should be in the actions/reducer.我认为问题应该出在actions/reducer上。

If step 2 does not work out then,如果步骤 2 不成功,那么,

Step 3) I would console.log the selectedCatCompanies state to validate if it is being recorded correctly in Redux.第 3 步)我将控制台记录 selectedCatCompanies state 以验证它是否在 Redux 中正确记录。 If not I would then go back to step 2.如果不是,我会 go 回到步骤 2。

If step 3 also does not work out then,如果步骤 3 也没有成功,那么,

Step 3) If you are successfully logging the selectedCatCompanies state then you can try to conditionally two different FlatLists instead of trying to conditionally render two different data sources.步骤 3) 如果您成功记录 selectedCatCompanies state,那么您可以尝试有条件地使用两个不同的 FlatList,而不是尝试有条件地呈现两个不同的数据源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM