When navigating from the previous page to that page, faceShape.map is not a function

byungju

I'm developing an app in react-native. I get the data by requesting get through api. What I want to do is to show it right away on the screen when it comes to that page from the previous page. However, when you come to the page, 'faceShape.map is not a function' error occurs. This is the code I wrote.

const [faceShape, setFaceShape] = useState();
  axios(config)
    .then(function (res) {
      let face = [];
      res.data.data.map((u) => {
        switch (u.faceType) {
          case "circle":
            face.push({ faceType: "둥근형", imgUrl: u.imgUrl });
            break;
          case "egg":
            face.push({ faceType: "계란형", imgUrl: u.imgUrl });
            break;
          case "square":
            face.push({ faceType: "사각형", imgUrl: u.imgUrl });
            break;
          case "reverseTriangle":
            face.push({ faceType: "역삼각형", imgUrl: u.imgUrl });
            break;
        }
      });
      setFaceShape(face);
    })
(...)
 {faceShape.map((data) => (
            <>
              <TouchableHighlight style={styles.skincolorcontainer}>
                <>
                  <SvgCssUri uri={data.imgUrl} width="50%" height="20%" />
                  <Text style={styles.skintext}>{data.faceType}</Text>
                </>
              </TouchableHighlight>
            </>
          ))}

I wonder why the data doesn't go over immediately when I first enter the page. Please give me an answer.

Kartikey Vaish

Try something like this

import { useEffect } from "react"; // At the top

(...)

const [faceShape, setFaceShape] = useState([]);

useEffect(() => {
  const unsubscribe = navigation.addListener("focus", () => {
    GetData();
  });

  return unsubscribe;
}, [navigation]);

const GetData = async () => {
  try {
    axios(config).then(function (res) {
      let face = [];
      res.data.data.map((u) => {
        switch (u.faceType) {
          case "circle":
            face.push({ faceType: "둥근형", imgUrl: u.imgUrl });
            break;
          case "egg":
            face.push({ faceType: "계란형", imgUrl: u.imgUrl });
            break;
          case "square":
            face.push({ faceType: "사각형", imgUrl: u.imgUrl });
            break;
          case "reverseTriangle":
            face.push({ faceType: "역삼각형", imgUrl: u.imgUrl });
            break;
        }
      });
      setFaceShape(face);
    });
  } catch (error) {}
};

(...)

{
  faceShape.map((data) => (
    <>
      <TouchableHighlight style={styles.skincolorcontainer}>
        <>
          <SvgCssUri uri={data.imgUrl} width="50%" height="20%" />
          <Text style={styles.skintext}>{data.faceType}</Text>
        </>
      </TouchableHighlight>
    </>
  ));
}

What I did here is that whenever this screen comes in focus it will fetch data and update it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

navigating back to the previous page in webview

Alerts when navigating away from a web page

Perform action when navigating away from a page

Select2 render multiple time when navigating to previous page

Navigating from Page/ Page Function to PageFunction Null Reference Exception occurs

JavaScript doesn't execute when navigating from page to page

How to resolve "stale element reference: element is not attached to the page document when navigating to the previous page"

ARCGis Xamarin - MapView from previous page displays after navigating to new page

Button x:Name="btnBack" not navigating to previous page

reset or clear the previous page after navigating

page data not showing when directly navigating to page

React, recovering from error when navigating to different page

Remember Cascading Dropdown value when navigating away from page

Lose the list values when navigating away from page

Delete cookie when navigating away from specific page of a website

Navigation Error when navigating to Home page from Firebase registration

How to remove the default 'Are you sure' popup when navigating from a page

jQuery file not found when navigating away from home page

Page does not change when navigating forward from second page to third page, which is the first page with the same url as the third page Angular Ionic

ERROR 404 popping up when navigating from my contacts page to my home page

How to create loader, when navigating from one page to another page not working

Skip "inbetween" pages when navigating back to the first page, from the last page in the navigation stack

How to add password when navigating to new page

Favicon disappears when navigating to a different page (in Chrome)

BottomNavigation does not update when navigating to new page

setState is not calling when Navigating to another page

Redux loses state when navigating to another page

Keep the values in page when navigating to other pages

BLOC losing context when navigating to a new page