0

Here is my issue: I created a glossary (a list with the 26 lettters and the words for each letter). I want to make it so that when I click on one of the letter (I have 26 buttons), it will lead to a scroll down to the letter in question. BUT the main issue is that the glossary doesn't change if the page is not refreshed. So when I click on a button, I can't use a if() in order to choose my letter (because the part creation of the glossary has been done). So how do I made my code dynamic? This question is the continuation of my other question : React Native Typescript : scroll with useRef and scrollIntoView. The code is there (basic version):

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

export default function App({ navigation }: { navigation: any }) {
  const fieldRef = React.useRef<null | HTMLInputElement>(null);
  const scrollToElement = () => fieldRef.current?.scrollIntoView();

  return (
    <View>
      <ScrollView>
        <View>
          <Text>Glossary</Text>
          <View>
            {letter.map((letter) => {
              if (letter.data.length >= 1) {
                return (
                  <View>
                    <Text
                      onPress={() => {
                        {
                          scrollToElement;
                        }
                      }}
                    >
                      {letter.name}
                    </Text>
                  </View>
                );
              }
            })}
          </View>

          <View>
            {letter.map((letter) => {
              return (
                <View>
                  <View>
                    <div
                      ref={fieldRef} //this is done at the loading of the page, not after
                    >
                      <Text>{letter.name}</Text>
                    </div>
                  </View>
                </View>
              );
            })}
          </View>
        </View>
      </ScrollView>
    </View>
  );
}
const letter = [
  {
    id: 1,
    name: "A",
    data: ["Abricot", "Abricot", "Abricot", "Artichaud", "Artichaud"],
    description: [
      "Un abricot est un fruit",
      "Un abricot est un fruit",
      "Un abricot est un fruit",
      "Un artichaud est un légume",
      "Un artichaud est un légume",
    ],
  },
  {
    id: 2,
    name: "B",
    data: ["Branche", "Branche", "Branche"],
    description: [
      "Une branche vient d'un arbre",
      "Une branche vient d'un arbre",
      "Une branche vient d'un arbre",
    ],
  },
  {
    id: 3,
    name: "C",
    data: [],
    description: [],
  },
];
Louis
  • 321
  • 2
  • 13
  • 1
    Hi. Please take the [tour], read [ask] and how to make a [mre]. Then [edit] your question accordingly. – super Jun 13 '22 at 15:04
  • I've added some code, I'm not sure what words to use to make the question better – Louis Jun 13 '22 at 15:21
  • 1
    You have 3 letters but only one ref. So that seems like an issue. Since you have an array for the letters I would store the ref there. One for each letter. – super Jun 13 '22 at 15:30
  • 1
    Have a look at [callback refs](https://reactjs.org/docs/refs-and-the-dom.html#callback-refs) for how to manage the ref updates easier. – super Jun 13 '22 at 15:34
  • Thanks a lot! This issue really blocked me – Louis Jun 13 '22 at 15:35

0 Answers0