简体   繁体   English

React JS 和 flask 功能组件在点击时呈现

[英]React JS and flask functional component render on click

I am trying to create a simple app with flask and react to display some data using recharts.我正在尝试使用 flask 创建一个简单的应用程序,并使用重新图表做出反应以显示一些数据。

This is what I have for the backend.这就是我对后端的看法。

import random
import flask as flask
from flask import jsonify

app = flask.Flask(__name__)
@app.route('/api/chart_data')
def getChartData():
    data = list(map(lambda _: {'x': random.random()*100, 'y': random.random()*100}, range(20)))
    return jsonify(data)

And for the react front end I have:对于反应前端,我有:

import React,  { useState, useEffect } from 'react';
import { ScatterChart, Scatter, XAxis, YAxis, CartesianGrid } from 'recharts';

export default function RandomPlot()
{
    const [data, setData] = useState([]);
    useEffect(() => {
        fetch('/api/chart_data')
        .then((res) => res.json())
        .then((data) => {
            console.log("Plot data is: ", data);
            setData(data);
        });
    }, []);

    return (
        <>
            <div>
                <button>Generate new data</button>
                <ScatterChart width={400} height = {400}>
                    <CartesianGrid />
                    <XAxis type="number" dataKey="x" />
                    <YAxis type="number" dataKey="y" />
                    <Scatter data={data} fille="green" />
                </ScatterChart>
            </div>
        </>
    );
}

Desired result: I want the front end to display new data when the user presses the "generate new data" button.期望的结果:我希望前端在用户按下“生成新数据”按钮时显示新数据。

What I have tried: I know that in the use effect hook you can pass in a values into the array so that the code runs every time one of the values changes.我尝试了什么:我知道在 use effect 挂钩中,您可以将一个值传递到数组中,以便代码在每次其中一个值更改时运行。 I tried adding an onClick function for the button that changes a variable that was passed into the use effect array.我尝试为更改传递到使用效果数组的变量的按钮添加 onClick function。 It didn't seem to work.它似乎没有用。

I am not sure what to pass into the useEffect array to get the desired result and how to write the onClick function.我不确定将什么传递到 useEffect 数组以获得所需的结果以及如何编写 onClick function。

I don't think you need the useEffect hook for this.我认为您不需要为此使用 useEffect 挂钩。 useEffect runs whenever the data in the array at the second argument changes, or if the array is empty, it will run when the page first loads. useEffect 在数组中第二个参数的数据发生变化时运行,或者如果数组为空,它将在页面首次加载时运行。

If you want to generate data whenever the user presses a button, then an onClick function should be enough.如果您想在用户按下按钮时生成数据,那么 onClick function 就足够了。 Let me know if it works, (By the way. this is my first time answering on StackOverflow so there maybe some formatting mistakes).让我知道它是否有效,(顺便说一句。这是我第一次在 StackOverflow 上回答问题,所以可能存在一些格式错误)。

import React,  { useState, useEffect } from 'react';
import { ScatterChart, Scatter, XAxis, YAxis, CartesianGrid } from 'recharts';

export default function RandomPlot()
{
    const [data, setData] = useState([]);

    function genData(){
    fetch('/api/chart_data')
    .then((res) => res.json())
    .then((data) => {
        console.log("Plot data is: ", data);
        setData(data);
}

    return (
        <>
            <div>
                <button onClick={genData}>Generate new data</button>
                <ScatterChart width={400} height = {400}>
                    <CartesianGrid />
                    <XAxis type="number" dataKey="x" />
                    <YAxis type="number" dataKey="y" />
                    <Scatter data={data} fille="green" />
                </ScatterChart>
            </div>
        </>
    );
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM