【问题标题】:Clearing value state in react清除反应中的值状态
【发布时间】:2017-10-20 23:22:43
【问题描述】:

使用 api 端点保存特定加密货币硬币的对象数组。

我创建了一个表单,用户可以在其中输入特定硬币并点击提交,它会返回价格。然后该硬币将检查它是否在 api 中的对象数组中。如果它有效,那么我将其推送到构造函数中的过滤结果数组中。

我的第一个搜索查询有效,但是当我进行第二个查询搜索并点击提交按钮时,它失败并重新加载页面。

  constructor() {
     super();
     this.state = {value: ''};
     this.state = {coin: []};
     this.state = {items: []};
     this.state = {filteredResults: []};

     this.handleChange = this.handleChange.bind(this);
     this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    let coin = this.state.value;
    this.findCoin(coin);
    event.preventDefault();
  }

  findCoin(id) {
    this.state.items.forEach(function(currency){
      if(currency.id === id) {
        this.state.filteredResults.push(currency)
      }
    }, this);

    this.setState({filteredResults: this.state.filteredResults[0]});
  }

  componentDidMount() {
    fetch(`https://api.coinmarketcap.com/v1/ticker/`)
      .then((result)=> {
        result.json()
      .then(json => {
        this.setState({items: json})
       });
    });
  }

  render() {
    return (
      <div className="App">
        <form onSubmit={this.handleSubmit}>
           <label>
             Name:
             <input type="text" value={this.state.value} onChange={this.handleChange} />
           </label>
           <input type="submit" value="Submit" />
        </form>
        <div> Price: $ {this.state.filteredResults.price_usd}
        </div>
      </div>
    );
  }
}

【问题讨论】:

  • 可能与您的问题无关,但您的构造函数中的状态设置可能看起来像这样:this.state = { value: '', coin: [], items: [], filteredResults: [] }

标签: javascript arrays reactjs filter react-redux


【解决方案1】:

这个方法是mutating state,绕过了React的状态检查;

  findCoin(id) {
    this.state.items.forEach(function(currency){
      if(currency.id === id) {
        this.state.filteredResults.push(currency)
      }
    }, this);

    this.setState({filteredResults: this.state.filteredResults[0]});
  }

使用过滤器等方法,提供新的数组引用:

const filtered = this.state.items.filter(ccy=> ccy.id === id);
this.setState({filteredResults: filtered[0]};

正如其他海报之一所提到的,将 filterResults 声明为一个对象(如果您只想显示一个过滤结果),因为它会从数组更改为对象。

this.state = {filteredResults: {}};

【讨论】:

  • 这不是唯一的问题,还有从数组到对象的数据类型变化
【解决方案2】:

这个方法的问题:

findCoin(id) {
    this.state.items.forEach(function(currency){
        if(currency.id === id) {
            this.state.filteredResults.push(currency)
        }
    }, this);

    this.setState({filteredResults: this.state.filteredResults[0]});
}

在线

this.setState({filteredResults: this.state.filteredResults[0]});

您正在将filteredResults(这是一个数组)设置为一个对象,并在第二次搜索该行

this.state.filteredResults.push(currency)

给你一个错误,因为filredResults 是一个字符串没有push 方法。

由于您在handleSubmit 方法的最后一行有event.preventDefault,由于先前的错误并且表单正在提交,因此它没有执行。

【讨论】:

  • 在第二次搜索时,当我放置一个调试器语句时,它看起来仍然是一个对象?
  • 已编辑。是的,它是 currency 对象,但不是预期的数组
猜你喜欢
  • 2018-08-16
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 2018-12-30
  • 2018-01-18
  • 2020-04-03
  • 1970-01-01
相关资源
最近更新 更多