简体   繁体   English

单击按钮不会更新我的React应用程序中的状态

[英]Button click won't update state in my React app

A button click shall filter my job-card array to only one category. 单击按钮应将我的工作卡阵列仅过滤到一个类别。 Eg button "Marketing" should filter to those jobs from array who have prop "jobstags: Marketing". 例如,“市场营销”按钮应筛选具有属性“工作标签:市场营销”的阵列中的那些工作。 I used a very similar procedure like for my input which filters jobs perfectly. 我对输入使用了非常类似的过程,可以完美地过滤作业。

I can console log my event (the button click) with the according value ("Marketing"). 我可以使用相应的值(“营销”)来控制台记录我的事件(单击按钮)。 But it still doesn't filter correctly... 但是它仍然不能正确过滤...

In my app I did this: 在我的应用程序中,我这样做:

export default class App extends Component {
  state = {
    jobs: jobs,
    searchfield: '',
    jobtags: ''
  }

  onSearchChange = event => {
    this.setState({ searchfield: event.target.value })
  }

  onClickChange = event => {
    console.log(event.target.value)
    this.setState({ jobtags: event.target.value })
  }

  render() {
    const filteredJobs = this.state.jobs.filter(job => {
      return (
        job.position
          .toLowerCase()
          .includes(this.state.searchfield.toLowerCase()) ||
        job.company
          .toLowerCase()
          .includes(this.state.searchfield.toLowerCase()) ||
        job.jobtags.toLowerCase().includes(this.state.jobtags.toLowerCase())
      )
    })

    // this.save()

    if (this.state.jobs.length === 0) {
      return <Loading>Loading...</Loading>
    } else {
      return (
        <Router>
          <React.Fragment>
            <Route
              exact
              path="/"
              render={() => (
                <Home
                  jobs={filteredJobs}
                  searchChange={this.onSearchChange}
                  clickChange={this.onClickChange}
                />
              )}
            />

onClickChange is what should update the state of tags onClickChange是应该更新tags状态的内容

In my Home component I then simply pass the value on to the Categories component: 然后,在我的Home组件中,只需将值传递给Categories组件:

<Categories clickChange={clickChange} />

Finally it arrives in my Categories component where I say: 最后,它到达我的类别组件,在其中我说:

export default class Categories extends Component {
  render() {
    const { clickChange } = this.props
    return (
      <Wrapper>
        <button value="Marketing" onClick={clickChange}>
          <img
            alt="Button"
            src={require('/Users/markus/Documents/q4-2018/jobs-app/src/img/computer.png')}
          />
          Frontend
        </button> ...

Any ideas? 有任何想法吗? Thx! 谢谢!

maybe you have to bind the "this" of "onClickChange", for example in the constructor of your App class. 也许您必须绑定“ onClickChange”的“ this”,例如在您的App类的构造函数中。

Example : 范例:

export default class App extends Component {
  constructor(props) {
  super(props);
  this.state = {
    jobs: jobs,
    searchfield: '',
    jobtags: ''
  };
  this.onClickChange = this.onClickChange.bind(this);

and it will work I think 我认为这会起作用

You will have to bind it. 您将不得不绑定它。 Add this line to your constructor: 将此行添加到您的构造函数中:

this.onClickChange = this.onClickChange.bind(this);

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

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