简体   繁体   English

如何在反应组件中加载新的 axios 数据

[英]How to load new axios data in react component

I've got a navigation menu that correctly loads components with react-router using <Link to={"/entity"}>Entities</Link>我有一个导航菜单,可以使用<Link to={"/entity"}>Entities</Link>正确加载带有 react-router 的组件

The components load data using axios and display it in tables.组件使用 axios 加载数据并将其显示在表格中。 What I'm struggling to accomplish is loading new data when clicking the <link> a subsequent time.我正在努力完成的是在随后单击<link>时加载新数据。

class List extends Component {
    constructor() {
        super();
        this.state = { entities: [], loading: true};
    }

    componentDidMount() {
        this.getData();
        console.log('componentDidMount');
        console.log(this.state.entities);
    }
    getData() {
        axios.get(this.props.url).then(response => {
            this.setState({ entities: response.data, loading: false})
        })
    }


    render() {
        ...
    }


This works well for loading a single set of data.这适用于加载一组数据。 But if I edit a row and open the list again it will have the originally retrieved.但是,如果我编辑一行并再次打开列表,它将获得最初检索的内容。 Which is the correct behaviour given that code, but how can I reload that?给定该代码,哪种行为是正确的,但我如何重新加载它? I've tried componentDidUpdate but that creates an infinite loop.我试过 componentDidUpdate 但这会造成无限循环。 I'm assuming due to componentDidUpdate changing the DOM which then again calls componentDidUpdate again.我假设由于 componentDidUpdate 更改了 DOM,然后再次调用 componentDidUpdate 。

    componentDidUpdate(prevProps) {
        this.getData();
        console.log('componentDidUpdate');
    }

I thought about doing something like adding onClick={this.handleClick} to the menus and changing states or passing values to indicate a new click.我想过在菜单中添加onClick={this.handleClick}并更改状态或传递值以指示新的点击。 But there must be a way to catch an update from router and not just a change of the component.但是必须有一种方法可以捕获来自路由器的更新,而不仅仅是组件的更改。

If you use setState inside componentDidUpdate it updates the component, resulting in a call to componentDidUpdate which subsequently calls setState again resulting in the infinite loop.如果您在componentDidUpdate使用setState ,它会更新组件,导致对componentDidUpdate的调用随后再次调用setState导致无限循环。 You should conditionally call setState and ensure that the condition violating the call occurs eventually eg:您应该有条件地调用setState并确保最终发生违反调用的条件,例如:

componentDidUpdate(previousProps, previousState) {
    if (previousProps.data !== this.props.data) {
        this.setState({/*....*/})
    }
}

The solution I came up with was to add a datestamp to the link and compare that to the previous timestamp.我想出的解决方案是在链接中添加一个日期戳,并将其与之前的时间戳进行比较。

<Link to={{"/entity", state: { update: + new Date() } }}>Entities</Link>
    componentDidUpdate(prevProps, prevState) {
        if (prevProps.update !== this.props.update) {
            this.setState({ loading: true })
            this.getData();
            //console.log('Data Updating - ' + this.props.update);
        }
    }

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

相关问题 组件安装时如何从 axios 获取数据 - How to get a data from axios when component mount in react 如何将 Axios 的响应数据存储到 React Functional Component 中的变量? - How to store response data from Axios to a variable in React Functional Component? 如何在反应中使用来自组件的数据加载页面 - How to load a page with the data from a component in react 将数据从 Axios 映射到 React 组件 - Mapping data from Axios to React component React.js:如何在新选项卡中加载 App 组件? - React.js: How to load the App component in a new tab? (React Native)在第一个数据集的末尾到达时如何加载新数据并添加到组件(回收者列表视图) - (React native) how to load new data and add to component (recycler list view) when reached at the end of first data set 如何使用 axios 在 React 中设置数据? - how to set data in React with axios? 使用新数据更新React组件 - Updating a React component with new data 如何从 Axios 获取数据并从 React 中的 Function 组件返回? - How do I get data from Axios and return it from a Function Component in React? 反应:当使用 axios 将 api 数据传递给子组件时,如何解决未定义的问题? - React: How do I fix getting undefined when passing api data to child component using axios?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM