【问题标题】:referenceError: Can't find variable in react-native with fetch参考错误:无法通过提取在本机反应中找到变量
【发布时间】:2020-05-13 09:46:52
【问题描述】:

嘿,我正在尝试使用来自反应本机的数据在线 json 数据的胜利来制作图表。该组件的代码如下所示

export default class Dashboard extends Component {
  static navigationOption = {
    title: 'DashboardScreen'
  }

state = {
      getValue: '',
      dataSource:[],
      isLoading: true
    }


  componentDidMount() {
  	
    const url = 'myurl';
    fetch(url, {
      method: 'GET',
      headers: new Headers({
        'Content-Type' : 'application/json',
      })
    })
    .then((response)=> response.json() )
    .then((responseJson) => {
      console.log(responseJson.Views)
      this.setState({
        dataSource: responseJson,
        isLoading: false
      })
    })
    .catch((Error) => {
      console.log(Error)
    })


  }

  render() {
      return(
          <View style = {styles.container}>
              <VictoryChart minDomain={{ y: 0, x:0 }} width={350} theme={VictoryTheme.material}>
                <VictoryBar
                    style={{
                      data: { stroke: "#c43a31" },
                      parent: { border: "1px solid #ccc"}
                    }}
                    data={[
                      { x: 1, y: 1 },
                      { x: 2, y: dataSource[0].views},
                      { x: 3, y: dataSource[1].views},
                      { x: 4, y: dataSource[2].views},
                      { x: 5, y: dataSource[3].views}
                      ]}> 
                </VictoryBar>
              </VictoryChart>
          </View>
      )
  }
  }

我认为 fetch 已经使 json 信息成为 dataSource 变量中的一个数组,并且可以用作胜利中的数据,但我总是遇到诸如 referenceError: Can't find variable 'dataSource' 之类的错误

所以,如果有人帮助我编写这段代码并建议如何将 json 数据制作成图表,那就太好了

【问题讨论】:

    标签: react-native fetch victory-charts


    【解决方案1】:

    改变:

      data={[
                          { x: 1, y: 1 },
                          { x: 2, y: dataSource[0].views},
                          { x: 3, y: dataSource[1].views},
                          { x: 4, y: dataSource[2].views},
                          { x: 5, y: dataSource[3].views}
                          ]}> 
    

      data={[
                          { x: 1, y: 1 },
                          { x: 2, y: this.state.dataSource[0].views},
                          { x: 3, y: this.state.dataSource[1].views},
                          { x: 4, y: this.state.dataSource[2].views},
                          { x: 5, y: this.state.dataSource[3].views}
                          ]}> 
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      • 1970-01-01
      • 2011-05-24
      • 2016-01-10
      • 1970-01-01
      相关资源
      最近更新 更多