TypeError :(中间值).bind不是函数,即使我已经绑定了函数

时间:2017-03-03 11:27:20

标签: reactjs onclick this bind

在axios函数中尝试setState({})时的onClick函数中,我收到以下错误消息:

TypeError: (intermediate value).bind is not a function

这是从setState({})函数中获得的。奇怪的是,我以正确的方式限制了函数(我认为),但它仍然给出错误信息:/

有谁知道可能出现的问题?

渲染功能

  render() {
    const { params, subcatItems } = this.props
    const { subcategory, category } = params
    const subcat = params ? subcategory : null

    return (
      <div id="subCategoryList">
        <ul>
          {this.renderDivs(subcatItems, subcat, category)}
        </ul>
      </div>
    )
  }

renderDivs()

  renderDivs(subcatItems, subcat, category) {
    let nestedElement = []

    if (this.state.seriesList.length>0) {
      nestedElement = this.renderNestedList()
    }

    let categoryList = []
    for (var item in subcatItems) {
      if (item != '_id') {
        if (subcatItems[item].parent==category) {
          console.log('RenderDivs', this.state)
          categoryList.push(<li
                              onClick={this.onClick.bind(this, item, subcatItems[item].parent)}
                              key={item}
                              className={subcat == replaceSpecialCharactersURLs(item) ? "active" : null}>
                              {subcatItems[item].name}
                              {nestedElement}
                            </li>)
        }
      }
    }
    return categoryList
  }

的onClick()

  onClick(subcategory, category) {
    browserHistory.push('/webshop/'+category+'/'+subcategory)

    axios.get('/products/subcategory/'+subcategory)
    .then(function (response) {
      let seriesList = []
      for (var index in response.data) {
        let seriesName = response.data[index].series
        if (seriesName) {
          seriesList.push(seriesName)
        }
      }

      this.setState({ seriesList: 'hej' }.bind(this))
    }.bind(this))
    .catch(function (error) {
      console.log(error);
    })
  }

1 个答案:

答案 0 :(得分:0)

因为bind(this)不需要setState

this.setState({ seriesList: 'hej' }.bind(this)) 

只需这样写:

this.setState({ seriesList: 'hej' }) 

使用这部分:

onClick(subcategory, category) {
    browserHistory.push('/webshop/'+category+'/'+subcategory);

    axios.get('/products/subcategory/'+subcategory)
    .then(function(response) {
        let seriesList = []
        for (var index in response.data) {
            let seriesName = response.data[index].series
            if (seriesName) {
                seriesList.push(seriesName)
            }
        }

        this.setState({ seriesList: 'hej' })
    }.bind(this))
    .catch(function (error) {
      console.log(error);
    })
}

或使用箭头功能:

onClick(subcategory, category) {
    browserHistory.push('/webshop/'+category+'/'+subcategory)

    axios.get('/products/subcategory/'+subcategory)
    .then(response => {
        let seriesList = []
        for (var index in response.data) {
            let seriesName = response.data[index].series
            if (seriesName) {
                seriesList.push(seriesName)
            }
        }

        this.setState({ seriesList: 'hej' });
    })
    .catch(error => {
        console.log(error);
    })
}

注意:不使用回调方法和.bind(this),而是使用箭头功能:

axios.get('/products/subcategory/'+subcategory)
.then(response => {
})
.catch(error => {
})