简体   繁体   English

React:如何重新渲染如果调用函数

[英]React : How to Re-Render If function is invoked

I am working on project , where I am updating the url . 我正在研究项目,我正在更新网址。 Actually It working fine when I click on next button but I want to re-render application when function is invoked . 实际上当我点击下一个按钮时工作正常,但我想在调用函数时重新渲染应用程序。 I am new to ReactJS , Could some one please help me how to solve this problem . 我是ReactJS的新手,有人可以帮我解决这个问题。

I am sorry I am not native speaker , I am sorry if I made mistake in English grammer . 对不起,我不是母语,如果我在英语语法中犯了错误,我很抱歉。 Thanks 谢谢

Displaying data through this URL Function 通过此URL功能显示数据

urlParams() {
 return `http://localhost:3001/meetups?filter[limit]=${(this.state.Item)}&&
filter[skip]=${this.state.skip}
&&filter[order]=${this.state.sortedData}`;
  }

Full Component Code 完整的组件代码

        class Example extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      Item: 5,
      skip: 0
    }

    this.handleClick = this.handleClick.bind(this);
  }

  urlParams() {
    return `http://localhost:3001/meetups?filter[limit]=${(this.state.Item)}&&filter[skip]=${this.state.skip}`
  }

  handleClick() {
    this.setState({skip: this.state.skip + 1})
  }

  render() {
    return (
      <div>
        <a href={this.urlParams()}>Example link</a>
        <pre>{this.urlParams()}</pre>
        <button onClick={this.handleClick}>Change link</button>
      </div>
    )
  }
}


ReactDOM.render(<Example/>, document.querySelector('div#my-example' ))

You can force a rerender of your component by calling: 可以通过调用以下命令强制重新呈现组件:

this.forceUpdate();

However, you have to be careful not to enter a never-ending update cycle. 但是,您必须小心不要进入永无止境的更新周期。

The usual (recommended) way to rerender is through the necessity of changing your state using setState . 通常(推荐)重新呈现的方式是使用setState更改状态的必要性。

EDIT 编辑

I think want you need is to call getData once you have clicked the table header: 我想你需要的是在你点击表头之后调用getData:

getSortedData=()=>{
  console.log("hello clicked")
  this.setState({
    sortedData:'companyName'
  })
  // now invoke getdata which will refresh the data:
  this.getData()
}

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

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