我正在尝试在我的应用程序中创建一个按钮,它将使用 React Native 按钮组件刷新图表中的随机数据(使用 react-native-chart-kit 制作)。我的代码如下:
import * as React from 'react';
import {useState} from 'react';
import { StyleSheet, Text, View, Switch, Dimensions, Button, Alert } from 'react-native';
import { LineChart } from "react-native-chart-kit";
import Header from "./components/Header";
import Colors from "./constants/colors";
export default function App() {
const [isEnabled, setIsEnabled] = useState(false);
const toggleSwitch = () => setIsEnabled(previousState => !previousState);
function refresh_data(){
...
//Code here?
...
}
return (
<View style = {styles.screen}>
<View style = {styles.button}>
<Button
title="Refresh"
color={Colors.primary}
onPress={() => refresh_data}
/>
</View>
<View style = {styles.graph}>
<View style = {{flex: 1}}>
<Text style = {styles.text}>Bezier Line Chart</Text>
</View>
<View style={{flex: 2}}>
<LineChart
data={{
labels: ["January", "February", "March", "April", "May", "June"],
datasets: [{
data: [
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100,
Math.random() * 100
]
}]
}}
width={Math.floor(Dimensions.get("window").width * 0.6)} // from react-native
height={220}
yAxisLabel="$"
yAxisSuffix="k"
yAxisInterval={1} // optional, defaults to 1
chartConfig={{
backgroundColor: "#e26a00",
backgroundGradientFrom: "#fb8c00",
backgroundGradientTo: "#ffa726",
decimalPlaces: 2, // optional, defaults to 2dp
color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
labelColor: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
style: {
borderRadius: 16
},
propsForDots: {
r: "6",
strokeWidth: "2",
stroke: "#ffa726"
}
}}
bezier
style={{
marginVertical: 8,
borderRadius: 16
}}
/>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
screen: {
flex: 1,
alignItems: 'stretch',
justifyContent: 'center'
},
button: {
alignSelf: 'center',
justifyContent: 'center',
flex: 1
},
graph: {
flex: 3,
alignItems: 'center',
justifyContent: 'center'
}
});
我对 Javascript 和 React 很陌生,所以这可能是一个非常基本的问题,我想学习比任何东西都多。有人认为我尝试过使用 location.reload() 重新加载整个页面,但这不是我想要的(我知道,因为它在应用程序的另一部分有一个开关,每当单击它时都会刷新数据,我只是无法弄清楚如何)。提前感谢您的帮助!