【问题标题】:Triggering refresh on function component with navigate (React Native)使用导航触发功能组件的刷新(React Native)
【发布时间】:2020-10-17 22:30:33
【问题描述】:

我的应用程序中的一个屏幕是一个功能组件,如下所示:

export default function DonationsScreen({ navigation }) {
  const [requests, setRequests] = useState("")

  useEffect(() => {
    if (!requests) {
      getRequests()
    }
  }, [])

  // I omit some of the code here

  return (
    <SafeAreaView>
      <SearchBar lightTheme placeholder="Type Here..." />
      <FlatList
        data={requests}
        renderItem={({ item }) =>
          item.donationsLeft > 0 ? (
            <DonationItem request={item} navigation={navigation} />
          ) : null
        }
        keyExtractor={(item) => item.id}
      />
    </SafeAreaView>
  )
}

class DonationItem extends React.Component {
  render() {
    const { navigate } = this.props.navigation
    return (
      <Card title="hello">
        <Button
          buttonStyle={styles.donateButton}
          onPress={() =>
            this.props.navigation.push("Realizar donación", {
              request: this.props.request,
            })
          }
          title="Donar"
        />
      </Card>
    )
  }
}

这样您在 DonationItem 中看到的 onPress 就会导航到另一个名为 DonationFormScreen 的屏幕。此屏幕中的内容并不重要,只是在其中一个方法中调用了另一个导航:

this.props.navigation.push('Donación confirmada', {comingFrom: this.state.comingFrom});

最后,“Donación confirmada”是一个屏幕 DonationConfirmationScreen:

export default class DonationConfirmationScreen extends React.Component {

    render() {

        return(
        <View style={styles.confirmation}>
            <View style={styles.confirmationContent}>
                <Ionicons name='ios-checkmark-circle' color={'green'} size={130}/>
                <Button
                buttonStyle={styles.button}
                title='Listo' 
                onPress={() => this.props.navigation.navigate("Pedidos")}
                />
            </View>
        </View>
        );
    }
}

如您所见,this.props.navigation.navigate("Pedidos") 在按钮内被调用并且工作正常。 我想要做的不仅是导航回“Pedidos”(这是我的 DonationsScreen),还要触发某种刷新,因为我需要再次调用 getRequests()。 我怎样才能做到这一点?

【问题讨论】:

    标签: javascript react-native


    【解决方案1】:

    感谢所有回答的人,但我最终使用了其他东西,因为我无法让其他东西工作。

    const isFocused = useIsFocused();
    
    const [requests, setRequests] = useState('');
    
    useEffect(() => {
      getRequests();
    } , [isFocused])
    

    如你所见,我最终使用了 isFocused。我必须添加这个导入:

    import { useIsFocused } from '@react-navigation/native';
    

    【讨论】:

      【解决方案2】:

      在您的情况下,您可以将方法 getRequests 与请求一起传递,例如在 DonationItem 组件中替换此行:

      onPress={() => this.props.navigation.push('Realizar donación', {request: this.props.request})}
      

      用这一行:

      onPress={() => this.props.navigation.push('Realizar donación', {request: this.props.request, getRequests: getRequests})}
      

      并继续传递它,直到到达目标屏幕,然后在完成后调用方法getRequests

      可选:考虑使用状态管理库来避免这些问题,例如:https://github.com/ctrlplusb/easy-peasy

      【讨论】:

        【解决方案3】:

        挂载时的查询请求

        export default function DonationsScreen({ route, navigation }) {  
          const { getRequests } = route?.params
        
          useEffect(async () => {
            if (getRequests) {
              const req = await getRequests() 
              setRequests(req)
            }
          }, [])
        

        也许将动态 getRequests 作为 route.param 传递

        onPress={() => { 
          this.props.navigation.push('Realizar donación', {
            request: this.props.request, 
            getRequests: getRequests
          })
        }
        

        【讨论】:

        • 我得到 TypeError undefined is not an object。问题是我第一次进入 DonationsScreen 它没有任何路由参数,所以我真的不知道如何检查。
        • 我认为 route?.params 中的问号可能意味着某种条件,但它似乎不起作用。
        • route?.params(可选链接)
        • TypeError undefined is not an object 意味着 getRequests in undefined in navigation.push 更多细节在这里:reactnavigation.org/docs/params
        猜你喜欢
        • 1970-01-01
        • 2019-02-19
        • 2020-05-30
        • 1970-01-01
        • 1970-01-01
        • 2015-08-26
        • 2016-07-16
        • 2019-03-30
        • 2020-05-23
        相关资源
        最近更新 更多