In React Native, how would you create next and go back buttons that navigate to the previous/next page from the current active page?

huangapple go评论63阅读模式
英文:

In React Native, how would you create next and go back buttons that navigate to the previous/next page from the current active page?

问题

这里是关于你所描述的应用的翻译:

这是一个简单的应用程序,其中有 5 个对象像列表/索引一样排列着,如果你点击其中一个,应用程序将导航到每个详细页面。

在每个页面上,有“下一个/返回”按钮。如果你点击“下一个”按钮,应用程序将导航到当前页面的下一个详细页面/对象。例如,如果你在第 3 页,将前往第 4 页;如果你在第 4 页,将前往第 5 页,依此类推。

这里是应用程序的屏幕截图。

这是 Home.js
[(https://i.stack.imgur.com/RbRgZ.png)](https://i.stack.imgur.com/Yg0MH.png)

这是我目前创建的代码。

people.js

const people = [
  {
    id: 1,
    name: "Clark",
  },
  {
    id: 2,
    name: "Terry",
  },
  {
    id: 3,
    name: "Vega",
  },
  {
    id: 4,
    name: "Ryu",
  },
  {
    id: 5,
    name: "Ken",
  },
];

export default people;

App.js

import * as React from "react";
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import Home from "./Home";
import EachPage from "./EachPage";

const Stack = createNativeStackNavigator();

function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={Home} />
        <Stack.Screen name="EachPage" component={EachPage} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

export default App;

Home.js

import React from "react";
import { View, Text, FlatList, TouchableOpacity } from "react-native";
import people from "./people";

function Home({ navigation }) {
  return (
    <View>
      <FlatList
        data={people}
        keyExtractor={(person) => person.id.toString()}
        renderItem={({ item }) => (
          <TouchableOpacity
            onPress={() => navigation.navigate("EachPage", item)}
          >
            <Text>{item.id}</Text>
            <Text>{item.name}</Text>
          </TouchableOpacity>
        )}
      />
    </View>
  );
}

export default Home;

EachPage.js

import React from "react";
import { View, Text, Button } from "react-native";

function EachPage({ route }) {
  return (
    <View>
      <Text>{route.params.id}</Text>
      <Text>{route.params.name}</Text>
      
      // 如果你点击“下一个”按钮,将前往下一个页面。
      // 因此,如果你访问的当前页面是第 2 页,那么在点击“下一个”按钮后,将前往第 3 页。
      <Button title="下一个" />
      <Button title="返回" />
    </View>
  );
}

export default EachPage;

我认为实现这个功能并不难,但我没有成功。即使我完成了两门课程,我也没有找到如何做到这一点。

你要如何实现具有导航到同一数组中另一个屏幕/对象功能的按钮?感谢你。

英文:

I'm new and tring to create a simple app, where 5 objects sit like a list/index, and if you press one of them, the app navigates to each of the detailed pages.
In each of the pages, there are next/goback buttons. If you press the next button, then the app navigates to the next detailed page/object from the current page where you are. For example, if you are in the 3rd page, you go to the 4th page. If you are in the 4th page, then you go to 5th page and so on.

Here's the screens of the app.

This is Home.js
[(https://i.stack.imgur.com/RbRgZ.png)](https://i.stack.imgur.com/Yg0MH.png)

Here's the cords that I've created so far.

people.js

const people = [
  {
    id: 1,
    name: &quot;Clark&quot;,
  },
  {
    id: 2,
    name: &quot;Terry&quot;,
  },
  {
    id: 3,
    name: &quot;Vega&quot;,
  },
  {
    id: 4,
    name: &quot;Ryu&quot;,
  },
  {
    id: 5,
    name: &quot;Ken&quot;,
  },
];

export default people;

App.js

import * as React from &quot;react&quot;;

import { NavigationContainer } from &quot;@react-navigation/native&quot;;
import { createNativeStackNavigator } from &quot;@react-navigation/native-stack&quot;;

import Home from &quot;./Home&quot;;
import EachPage from &quot;./EachPage&quot;;

const Stack = createNativeStackNavigator();

function App() {
  return (
    &lt;NavigationContainer&gt;
      &lt;Stack.Navigator&gt;
        &lt;Stack.Screen name=&quot;Home&quot; component={Home} /&gt;
        &lt;Stack.Screen name=&quot;EachPage&quot; component={EachPage} /&gt;
      &lt;/Stack.Navigator&gt;
    &lt;/NavigationContainer&gt;
  );
}

export default App;

Home.js

import React from &quot;react&quot;;
import { View, Text, FlatList, TouchableOpacity } from &quot;react-native&quot;;

import people from &quot;./people&quot;;

function Home({ navigation }) {
  return (
    &lt;View&gt;
      &lt;FlatList
        data={people}
        keyExtractor={(person) =&gt; person.id}
        renderItem={({ item }) =&gt; (
          &lt;TouchableOpacity
            onPress={() =&gt; navigation.navigate(&quot;EachPage&quot;, item)}
          &gt;
            &lt;Text&gt;{item.id}&lt;/Text&gt;
            &lt;Text&gt;{item.name}&lt;/Text&gt;
          &lt;/TouchableOpacity&gt;
        )}
      /&gt;
    &lt;/View&gt;
  );
}

export default Home;

EachPage.js

import React from &quot;react&quot;;
import { View, Text, Button } from &quot;react-native&quot;;

function EachPage({ route }) {
  return (
    &lt;View&gt;
      &lt;Text&gt;{route.params.id}&lt;/Text&gt;
      &lt;Text&gt;{route.params.name}&lt;/Text&gt;
      
      //If you press the Next button, you go to the next page.
      //So, if the current page you visit is the 2nd page, then you go to the 3rd page after pressing the Next button.
      &lt;Button title=&quot;Next&quot; /&gt;
      &lt;Button title=&quot;Back&quot; /&gt;
    &lt;/View&gt;
  );
}

export default EachPage;

I think I know this shouldn't be that difficult, but I couldn't achive this. Even though I've finished two full courses, I didn't find how to do this.

How would you implement the buttuns that have the function that navigate to another screen/object in the same array in this case?
Thank you.

答案1

得分: 2

我删除了禁用的逻辑,现在数据实际上在循环导航中,既可以前进又可以后退。

import React, { useState, useEffect } from "react";
import { View, Text, Button } from "react-native";
import people from "./people";

function EachPage({ route, navigation }) {
  const [next, setNext] = useState(null);
  const [back, setBack] = useState(null);
  const [currentIndex, setCurrentIndex] = useState(null);

  useEffect(() => {
    setCurrentIndex(people.findIndex((item) => item.id === route.params.id));
  }, [route]);

  useEffect(() => {
    setNext(currentIndex + 1);
    setBack(currentIndex - 1);
    if (currentIndex === 0) {
      setBack(people.length - 1);
    }
    if (currentIndex + 1 === people.length) {
      setNext(0);
    }
  }, [currentIndex]);

  return (
    <View>
      <Text>{route.params.id}</Text>
      <Text>{route.params.name}</Text>
      {/* //如果按下“下一页”按钮,您将前往下一页。 //因此,如果您访问的当前页面是第2页,则按下“下一页”按钮后,您将前往第3页。 */}
      <Button
        title="Next"
        onPress={() => navigation.navigate("EachPage", people[next])}
      />
      <Button
        title="Back"
        onPress={() => navigation.navigate("EachPage", people[back])}
      />
    </View>
  );
}

export default EachPage;
英文:

I took out the disabled logic and now essentially the data is in circular navigation for both next and back.

import React, { useState, useEffect } from &quot;react&quot;;
import { View, Text, Button } from &quot;react-native&quot;;
import people from &quot;./people&quot;;

function EachPage({ route, navigation }) {
  const [next, setNext] = useState(null);
  const [back, setBack] = useState(null);
  const [currentIndex, setCurrentIndex] = useState(null);

  useEffect(() =&gt; {
    setCurrentIndex(people.findIndex((item) =&gt; item.id === route.params.id));
  }, [route]);

  useEffect(() =&gt; {
    setNext(currentIndex + 1);
    setBack(currentIndex - 1);
    if (currentIndex === 0) {
      setBack(people.length - 1);
    }
    if (currentIndex + 1 === people.length) {
      setNext(0);
    }
  }, [currentIndex]);

  return (
    &lt;View&gt;
      &lt;Text&gt;{route.params.id}&lt;/Text&gt;
      &lt;Text&gt;{route.params.name}&lt;/Text&gt;
      {/* //If you press the Next button, you go to the next page. //So, if the
      current page you visit is the 2nd page, then you go to the 3rd page after
      pressing the Next button. */}
      &lt;Button
        title=&quot;Next&quot;
        onPress={() =&gt; navigation.navigate(&quot;EachPage&quot;, people[next])}
      /&gt;
      &lt;Button
        title=&quot;Back&quot;
        onPress={() =&gt; navigation.navigate(&quot;EachPage&quot;, people[back])}
      /&gt;
    &lt;/View&gt;
  );
}

export default EachPage;

答案2

得分: 0

以下是代码部分的翻译:

EachPage.js

import React from "react";
import { View, Text, Button } from "react-native";
import people from "./people";

function EachPage({ route, navigation }) {
  let currentIndex = people.findIndex((item) => item.id === route.params.id);

  return (
    <View>
      <Text>{route.params.id}</Text>
      <Text>{route.params.name}</Text>
      {/* //如果你按下“下一页”按钮,你将前往下一页。 //因此,如果你当前访问的页面是第二页,那么在按下“下一页”按钮后,你将前往第三页。 */}
      <Button
        title="下一页"
        disabled={currentIndex + 1 === people.length}
        onPress={() => navigation.navigate("EachPage", people[currentIndex + 1])}
      />
      <Button
        title="返回"
        disabled={currentIndex === 0}
        onPress={() => navigation.navigate("EachPage", people[currentIndex - 1])}
      />
    </View>
  );
}

export default EachPage;
英文:

This is one possible solution, I tried it and worked for me:

EachPage.js

import React from &quot;react&quot;;
import { View, Text, Button } from &quot;react-native&quot;;
import people from &quot;./people&quot;;

function EachPage({ route, navigation }) {
  let currentIndex = people.findIndex((item) =&gt; item.id === route.params.id);

  return (
    &lt;View&gt;
      &lt;Text&gt;{route.params.id}&lt;/Text&gt;
      &lt;Text&gt;{route.params.name}&lt;/Text&gt;
      {/* //If you press the Next button, you go to the next page. //So, if the
      current page you visit is the 2nd page, then you go to the 3rd page after
      pressing the Next button. */}
      &lt;Button
        title=&quot;Next&quot;
        disabled={currentIndex + 1 === people.length}
        onPress={() =&gt;
          navigation.navigate(&quot;EachPage&quot;, people[currentIndex + 1])
        }
      /&gt;
      &lt;Button
        title=&quot;Back&quot;
        disabled={currentIndex === 0}
        onPress={() =&gt;
          navigation.navigate(&quot;EachPage&quot;, people[currentIndex - 1])
        }
      /&gt;
    &lt;/View&gt;
  );
}

export default EachPage;

huangapple
  • 本文由 发表于 2023年2月19日 11:26:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定