简体   繁体   English

React textfield onChange事件

[英]React textfield onChange event

Let me explain what I'm trying to do : I have a page, that present information from a web service . 让我解释一下我要做的事情:我有一个页面,它提供来自Web服务的信息。 I've also used the web service paging (send the desired page in the url params) for my TablePagintation so I won't store unneccesary information . 我还使用了Web服务分页(在url参数中发送所需的页面)用于我的TablePagintation因此我不会存储不必要的信息。

Now I also wan't to filter my data . 现在我也不想过滤我的数据。 I can't simply filter the result returned from the web service , since it only contains 10 records(because of the paging method) , so I need to call the web service again, but this time with a search query and it will return the filtered result (which should also be presented with paging via tablePagintation) 我不能简单地过滤从Web服务返回的结果,因为它只包含10条记录(因为分页方法),所以我需要再次调用Web服务,但这次使用搜索查询,它将返回过滤结果(也应通过tablePagintation进行分页)

The problem is, I don't know how I can reach my current state filter, inside the method the calls the web service .. 问题是,我不知道如何在调用Web服务的方法中达到我当前的状态过滤器。

My text field : 我的文字字段:

  <TextField
    id="filter"
    helperText={labels.filterHelperText}
    value={props.brokersListFilter}
    onChange={props.setBrokersListFilter}
    className={props.classes.textField}/>

My setBrokersListFilter : 我的setBrokersListFilter

export const setBrokersListFilter = (event) => {
    return dispatch => {
      dispatch({
        type: actionNames.SET_BROKERSLIST_FILTER,
        brokersListFilter: event.target.value
      })
    }
}    

So far, its correctly updating the state , but now I need to call my method getBrokers , with the state filter to recall the web service . 到目前为止,它正确地更新了状态,但现在我需要调用我的方法getBrokers ,使用状态过滤器来调用Web服务。

export const getBrokers = (page) => {
  return async dispatch => {
    try {
      filter = filter ? filter : '' // HERE I WANT THE STATE FILTER AS DEFAULT
      dispatch({type: appActionNames.TOGGLE_LOADING})
      const res = await fetch(`/...?pageNumber=${page + 1}&pageSize=10&searchQuery=${filter}`, {
        method: 'GET',
        headers: {
          "Accept": "application/json; charset=utf-8"
        }
      })
      .....

So how can I access the state in these methods , so I won't override the existing filter (while changing a page or the filter) 那么如何在这些方法中访问状态,所以我不会覆盖现有的过滤器(在更改页面或过滤器时)

What can I do ? 我能做什么 ?

I've managed to solve the problem by adding a handler that will call both methods . 我设法通过添加一个将调用这两个方法的处理程序来解决问题。

onChange handler : onChange处理程序:

<TextField
  id="filter"
  helperText={labels.filterHelperText}
  value={props.brokersListFilter}
  onChange={props.onFilterChange }
  className={props.classes.textField}/>
........
onFilterChange = (event) => {
  this.props.setBrokersListFilter(event)
  this.props.getBrokers(this.props.brokersListPage, this.props.brokersListFilter)
}

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

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