react本地选项卡视图渲染api呼叫后

发布于 2025-02-13 03:51:00 字数 1711 浏览 3 评论 0原文

我正在使用Tab View React本机库来构建用户帐户屏幕。我的请求很简单,如何在获取用户数据的API调用后更新选项卡视图内容?

function UserStoreScreen({ navigation, route }) {
  const layout = useWindowDimensions();
  const [index, setIndex] = React.useState(0);
  const [userStore, setUserStore] = React.useState({});
  const [routes] = React.useState([
    { key: "first", title: "Dressing" },
    { key: "second", title: "À propos" },
  ]);
  const user = route.params;

  // renders undefined 
  const FirstRoute = () => (
    <>
      <View style={styles.userContainer}>
        <ListItem
          image={`${IMAGES_BASE_URL}${userStore.photo}`}
          title={`${userStore.username}`}
          subTitle={`${userStore.store.length} Articles`}
        />
      </View>
    </>
  );

  const SecondRoute = () => (
    <>
      <View style={{ flex: 1, backgroundColor: "#ff4081" }} />
    </>
  );

  const renderScene = SceneMap({
    first: FirstRoute,
    second: SecondRoute,
  });

  const getUser = async () => {
    await axiosApi
      .post("/getUserProducts", { user_id: user.user_id })
      .then((response) => {
       // didn't work since set state is async 
        setUserStore(response.data);
        
      })
      .catch((err) => {
        console.log(err);
      });
  };
  // Get store products
  useEffect(() => {
    getUser();
  }, []);

  return (
    <Screen style={styles.screen}>
      <TabView
        navigationState={{ index, routes }}
        renderScene={renderScene}
        onIndexChange={setIndex}
        initialLayout={{ width: layout.width }}
      />
    </Screen>
  );
}

在接收API调用数据后,有没有办法使TAB视图的内容更新?

i'm using the tab view react native library to build a user account screen. my request is simple, how can i update the tab view content after an api call that fetches the user data?

function UserStoreScreen({ navigation, route }) {
  const layout = useWindowDimensions();
  const [index, setIndex] = React.useState(0);
  const [userStore, setUserStore] = React.useState({});
  const [routes] = React.useState([
    { key: "first", title: "Dressing" },
    { key: "second", title: "À propos" },
  ]);
  const user = route.params;

  // renders undefined 
  const FirstRoute = () => (
    <>
      <View style={styles.userContainer}>
        <ListItem
          image={`${IMAGES_BASE_URL}${userStore.photo}`}
          title={`${userStore.username}`}
          subTitle={`${userStore.store.length} Articles`}
        />
      </View>
    </>
  );

  const SecondRoute = () => (
    <>
      <View style={{ flex: 1, backgroundColor: "#ff4081" }} />
    </>
  );

  const renderScene = SceneMap({
    first: FirstRoute,
    second: SecondRoute,
  });

  const getUser = async () => {
    await axiosApi
      .post("/getUserProducts", { user_id: user.user_id })
      .then((response) => {
       // didn't work since set state is async 
        setUserStore(response.data);
        
      })
      .catch((err) => {
        console.log(err);
      });
  };
  // Get store products
  useEffect(() => {
    getUser();
  }, []);

  return (
    <Screen style={styles.screen}>
      <TabView
        navigationState={{ index, routes }}
        renderScene={renderScene}
        onIndexChange={setIndex}
        initialLayout={{ width: layout.width }}
      />
    </Screen>
  );
}

is there a way to make the content of the tab view updated after i receive the data from the api call?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千笙结 2025-02-20 03:51:00

是的,有一种有力重新安装组件的方法。为此,我们可以使用 prop类似:

return (
    <Screen style={styles.screen}>
      <TabView
        key={JSON.stringify(userStore)}
        navigationState={{ index, routes }}
        renderScene={renderScene}
        onIndexChange={setIndex}
        initialLayout={{ width: layout.width }}
      />
    </Screen>
  );

props如何工作?每当组件重新渲染时,它将检查值是否相同。如果不是相同的,请迫使组件重新渲染。

在这种情况下,我们将始终检查userstore值是否已更改。

Yes, there is a way to forcefully re-mount a component. To do that, we can use key props like this:

return (
    <Screen style={styles.screen}>
      <TabView
        key={JSON.stringify(userStore)}
        navigationState={{ index, routes }}
        renderScene={renderScene}
        onIndexChange={setIndex}
        initialLayout={{ width: layout.width }}
      />
    </Screen>
  );

How does key props work? Every time a component is re-rendering, it will check whether the key value is the same or not. If it's not the same, then force a component to re-render.

In this case we will always check if userStore value has changed or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文