简体   繁体   English

React Native Webscraping Attempt:undefined 不是一个函数(靠近 '...this.state.items.map...')

[英]React Native Webscraping Attempt: undefined is not a function (near '...this.state.items.map...')

I'm new to react native (Using Expo; I'm trying to learn it for a project I'll be doing with someone else) and I've been struggling with this for a couple of hours.我是本地反应的新手(使用世博会;我正在尝试为我将与其他人一起做的项目学习它)并且我已经为此苦苦挣扎了几个小时。 I have searched for solutions to this error but I could not figure it out.我已经搜索了此错误的解决方案,但我无法弄清楚。

I have been following this tutorial to get some webscraping to work.我一直在关注教程来获得一些网页抓取工作。 The end goal is not an amazon list like the tutorial, but it should get the job done.最终目标不是像教程那样的亚马逊列表,但它应该完成工作。 I am using cheerio-without-node-native as the webscraping library.我使用cheerio-without-node-native 作为webscraping 库。

I'm getting the error undefined is not a function (near '...this.state.items.map...') when trying to run the app.尝试运行应用程序时,我收到错误undefined is not a function (near '...this.state.items.map...') This is the file I'm working on:这是我正在处理的文件:

  import { TouchableOpacity, ScrollView, StyleSheet } from 'react-native';
  import { ExpoLinksView } from '@expo/samples';
  import cio from 'cheerio-without-node-native';


  async function loadGraphicCards(page = 1) {
    const searchUrl = `https://www.amazon.de/s/?page=${page}&keywords=graphic+card`;
    const response = await fetch(searchUrl);  // fetch page 

    const htmlString = await response.text(); // get response text
    const $ = cio.load(htmlString);       // parse HTML string

    return $("#s-results-list-atf > li")             // select result <li>s
      .map((_, li) => ({                      // map to an list of objects
        asin: $(li).data("asin"),    
        title: $("h2", li).text(),    
        price: $("span.a-color-price", li).text(),
        rating: $("span.a-icon-alt", li).text(),
        imageUrl: $("img.s-access-image").attr("src")
      }));
}

  export default function LinksScreen() {

      state = { 
          page: 1,
          items: [], 
      };

      state.items = loadGraphicCards(state.page);

    return (
      // <ScrollView style={styles.container}>
      <ScrollView>
          {this.state.items.map(item => <Item {...item} key={item.asin}/>)}
      </ScrollView>
    );
  }

  LinksScreen.navigationOptions = {
    title: 'Readings',
  };

  const Item = props => (
      <TouchableOpacity onPress={() => alert("ASIN:" + props.asin)}>
          <Text>{props.title}</Text>
          <Image source={{uri: props.imageUrl}}/>
          <Text>{props.price}</Text>
          <Text>{props.rating}</Text>
      </TouchableOpacity>
  )

  const styles = StyleSheet.create({
    container: {
      flex: 1,
      paddingTop: 15,
      backgroundColor: '#ffffff',
    },
  });

I have already tried putting 'this' in various places, and while that may very well be the problem, those solutions were not working for me.我已经尝试将“this”放在不同的地方,虽然这很可能是问题所在,但这些解决方案对我不起作用。

In your answer, can you please explain what was wrong, why, and if possible give resources to learn more?在您的回答中,您能否解释一下哪里出了问题,为什么,并在可能的情况下提供资源以了解更多信息?

Thanks!谢谢!

actually you are trying to use state like class component in a functional component.实际上,您正在尝试在功能组件中使用类组件之类的状态。 So if I where you I'd do something like:所以如果我在你那里我会做这样的事情:

import React, { useState } from 'react';
//.....
export default function LinksScreen() {
    const [items, setItems] = useState([]);

    setItems(YOUR_ITEMS_FROM_SOMEWHERE));

    return (
      // <ScrollView style={styles.container}>
      <ScrollView>
          {items && items.map(item => <Item {...item} key={item.asin}/>)}
      </ScrollView>
    );
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native TypeError: undefined is not a function (near &#39;...this.state.clubs.map...&#39;) - React Native TypeError: undefined is not a function (near '...this.state.clubs.map...') React-Native TypeError:未定义不是函数(在“ ... this.state.feeds.map ...”附近) - React-Native TypeError: undefined is not a function (near '…this.state.feeds.map…') React native Undefined 不是函数(&#39;...data.map...&#39; 附近) - React native Undefined is not a function (near '...data.map...') React Native TypeError undefined is not a function(靠近'...data.map...') - React Native TypeError undefined is not a function (near '…data.map…') TypeError: undefined is not a function (near &#39;...productsCtg.map...&#39;) 反应原生 - TypeError: undefined is not a function (near '...productsCtg.map...') react native undefined is not a function (near '...data.map...') |React native - undefined is not a function (near '…data.map…') |React native undefined 不是一个函数(靠近&#39;...icons.map...&#39;)React Native - undefined is not a function (near '...icons.map...') React Native Undefined 不是 function('...item.map...' 附近)-React Native 搜索栏 - Undefined is not a function (near '…item.map…')-React Native search bar undefined 不是 function ('...this.state.dataBanner.map...'附近) - undefined is not a function (near '...this.state.dataBanner.map...') undefined 不是 function ('...this.state.results.map...' 附近) - undefined is not a function (near '…this.state.results.map…')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM