0

我是 React Native 的新手。实际上这是我的第一个应用程序。

我一直在尝试将数据从 Fetch 传递到组件,但没有运气。我已经上下左右研究了谷歌,尝试了数百万种东西,但仍然没有成功。

这是我的 App.js

import React, {useState, useEffect} from 'react';
import { View, StyleSheet, ScrollView } from 'react-native';

import Header from './parts/header';
// import BigNews from './parts/big-news';
import Index from './screens/index.js';

const authorization = { authorization: '1234aa' };

export default function App() {
  const [fetchApiData, setApiData] = useState();

  useEffect(() =>
  fetch('https://example.com/get-app-home.php', {
    method: 'POST', // or 'PUT'
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(authorization),
  })
  .then(res => res.json())
  .then(res => {
    this.setState({ setApiData: res })
  })
  .catch(() => this.setState({ hasErrors: true }))
  );
  return (
    <View style={styles.viewport}>
      <Header />
      <ScrollView style={styles.contentHolder}>
        <Index content={fetchApiData}/>
      </ScrollView>
    </View>
);


}

这是我的 index.js 文件

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

import BigNews from '../parts/big-news';
import SmallNews from '../parts/small-news';
import Colors from '../constants/colors'

const Index = props => {

    return (
      <View>
        <BigNews
        image={props.content.first.image}
        pretitle={props.content.first.pretitle}
        title={props.content.first.title}
        introduction={props.content.first.introduction}
        style={{color: props.content.first.mark}}/>

        <SmallNews
        image={props.content.second.image}
        pretitle={props.content.second.pretitle}
        title={props.content.second.title}
        style={{color: props.content.first.mark}}/>

        <SmallNews
        image={props.content.second.image}
        pretitle={props.content.second.pretitle}
        title={props.content.second.title}
        style={{color: props.content.first.mark}}/>

        <SmallNews
        image={props.content.second.image}
        pretitle={props.content.second.pretitle}
        title={props.content.second.title}
        style={{color: props.content.first.mark}}/>
      </View>
    );
}
export default Index;

我经常收到 TypeError: undefined is not an object (evalating 'props.content.first')

我尝试了数百万种方法,但大多数时候这是我看到的错误。如果我执行 console.log of state 我得到未定义或空的。

4

2 回答 2

1

您没有将数据设置this.setState为类组件。替换this.setStatesetApiData()

useState返回初始状态和更新该值的函数。

const [ hasError, setHasErrors] = useState(false);

.then(res => res.json())
.then(res => {
  setApiData(res)
})
.catch(() => setHasErrors(true))
);

正如乔治所指出的,初始状态设置为空,做props.content.first.image会导致错误。将初始值设置为对象,例如

const [ fetchApiData, setApiData ] = useState({
   first: {},
   second: {}
});

或验证组件中的值。

const Index = props => {

  return (
   <View>
    <BigNews
    image={props && props.content && props.content.first && props.content.first.image}
    pretitle={props && props.content && props.content.pretitle && props.content.first.pretitle}
    title={props && props.content && props.content.title && props.content.first.title}
    introduction={props && props.content && props.content.introduction && props.content.first.introduction}
    style={{color: props && props.content && props.content.first && props.content.first.mark}}/>

    ...
  </View>
  );
}
export default Index;
于 2020-02-17T20:56:26.413 回答
1

在您的Index组件中,contentprop 最初是未定义的,因为您没有将任何值(通常称为“初始状态”)传递给组件中的useState函数App

相反,请尝试:

const [ fetchApiData, setApiData ] = useState({
    first: {},
    second: {}
});

请注意,我已经用firstsecond项填充了初始状态,因为尝试访问树下方的元素(imagepretitle等)仍然会引发错误。

于 2020-02-17T21:00:02.983 回答