简体   繁体   English

如何一次又一次停止渲染中的函数

[英]How to stop function again and again calling in render

I am new to react, I am getting data from redux, first, I get an object from accounts from redux, then I pass this to the function in redux and set a value in numReg in the reducer. 我是新来的反应者,我从redux获取数据,首先,我从redux的帐户中获取一个对象,然后将其传递给redux中的函数,并在化numReg中的numReg中设置一个值。

When I call a function by this.props.fetchAccountDetail(data) in actions its send a request to API and fetch the data from API and save it in reducer or store. 当我在操作中通过this.props.fetchAccountDetail(data)调用函数时, this.props.fetchAccountDetail(data)向API发送请求并从API获取数据,然后将其保存在reducer或store中。 When i call function in render by this.getDataFromAccount(accountDetail.num) , it goes in infinite loop . 当我通过this.getDataFromAccount(accountDetail.num)调用渲染函数时,它进入无限循环

I want data in a return, it should only run one time. 我想要返回数据,它只能运行一次。

import React, { Component } from 'react'
import { fetchAccountDetail, } from '../../../actions'

class myclass extends Component {
  state = {
    num : ''
  };

  getAccounts = (data) => {
    if (!data) { return; }
    return data.find(item => item.id == this.props.match.params.id);
  }

   getDataFromAccount = (data) => {
      this.props.fetchAccountDetail(data); 
      // This is a api , which provide the result agaisnt 
      // a num and set value in numReg in reducer
   }

  render() {
    const { accounts, numReg } = this.props;
    const accountDetail = this.getAccounts(accounts);
    // Here i will get a match object like  {id :1 , num :12345}

    const test=this.getDataFromAccount(accountDetail.num)
    // When i call this , it stucks in infinite loop , how can i get data only once when it render

    console.log(test)       

    return (
      <div />
    );
  }
}

const mapStateToProps = state => {
  return { accounts : state.accounts.accounts | [{id :1 , num :12345} , {id :2 , num :535234}],
    numReg : state.accounts.numReg  
    //Is a object containg the information of num from accounts
  }
}

export default (compose(
  withStyles(styles),
  connect(mapStateToProps, { fetchAccountDetail,}))(myclass));

It should return data in variable test after fetching data from redux. 从redux获取数据后,它应该在变量test中返回数据。

You should never call data fetching functions or functions which alter the state within render. 永远不要调用数据获取函数或会改变渲染状态的函数。

Render may be called multiple times if a parent rerenders or just its internal state changes. 如果父母重新渲染或仅其内部状态发生变化,则可以多次调用渲染。 Calling fetchAccountDetails in render updates the redux store. 在render中调用fetchAccountDetails将更新redux存储。 Redux will pass the new but equal data as props into your component. Redux会将新的但相等的数据作为prop传递到组件中。

That Component will rerender because its props changed and will call fetchAccountDetails again => loop. 该组件将重新渲染,因为其道具发生了变化,并将再次调用fetchAccountDetails =>循环。 Render should only display data!! 渲染器应该只显示数据!!

For data fetching, 2 functions exist. 对于数据获取,存在2个功能。 componentDidMount which will be called after the component is visible. componentDidMount将组件之后被称为是可见的。 That would be a good place to call your fetch. 那将是调用您的访存的好地方。

If you need a prop to fetch the data for eg an Id of some sort (fetch data for that Id), you would use componentDidUpdate in which you compare the new id and the old id to see if you need to fetch the data again. 如果您需要一个prop来获取数据( 例如某种ID)(获取该ID的数据),则可以使用componentDidUpdate在其中比较新ID和旧ID以查看是否需要再次获取数据。

You should read the docs and look at some tutorials. 您应该阅读文档并查看一些教程。 Hope this helps. 希望这可以帮助。

Happy coding. 快乐的编码。

As Domino987 answered, you need to make use of lifecycle methods . 正如Domino987回答的那样,您需要使用生命周期方法 Here's an example of how it might look: 下面是它可能如何看一个例子:

componentDidMount() {
  const { accounts } = this.props;
  const accountDetail = this.getAccounts(accounts);
  const accountData = this.getDataFromAccount(accountDetail.num)
  this.setState({
    account: {
      accountDetail: accountDetail,
      accountData: accountData
    }
  })
}

componentDidUpdate() {
  const { accounts } = this.props;
  const accountDetail = this.getAccounts(accounts);
  const accountData = this.getDataFromAccount(accountDetail.num)
  if (this.state.account.accountData !== this.getDataFromAccount(accountDetail.num)) {
    this.setState({
      account: {
        accountDetail: accountDetail,
        accountData: accountData
      }
    })
  }
}

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

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