Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 无法在表单中使用函数 构造函数(道具){ 超级(道具); this.submitQA=this.submitQA.bind(this); this.onSearchChange=this.onSearchChange.bind(this); this.isSearched=this.isSearched.bind(this); this.answerSubmitted=this.answerSubmitted.bind(this); 此.state={ 答复:[], 答复:",, 搜索词:“”, } } 答复(事件){ event.preventDefault(); const input=event.target.querySelector('input'); 常量值=input.value; const updatedList=this.state.answer; updatedList.push(值); this.setState({answer:updatedList}); } render(){ 返回( 搜索: 问答: 提交 {basicFormat} {this.state.answers.filter(this.isSearched(this.state.searchTerm)).map(函数(项){ 返回( {item} ) } ) } ); } }_Reactjs - Fatal编程技术网

Reactjs 无法在表单中使用函数 构造函数(道具){ 超级(道具); this.submitQA=this.submitQA.bind(this); this.onSearchChange=this.onSearchChange.bind(this); this.isSearched=this.isSearched.bind(this); this.answerSubmitted=this.answerSubmitted.bind(this); 此.state={ 答复:[], 答复:",, 搜索词:“”, } } 答复(事件){ event.preventDefault(); const input=event.target.querySelector('input'); 常量值=input.value; const updatedList=this.state.answer; updatedList.push(值); this.setState({answer:updatedList}); } render(){ 返回( 搜索: 问答: 提交 {basicFormat} {this.state.answers.filter(this.isSearched(this.state.searchTerm)).map(函数(项){ 返回( {item} ) } ) } ); } }

Reactjs 无法在表单中使用函数 构造函数(道具){ 超级(道具); this.submitQA=this.submitQA.bind(this); this.onSearchChange=this.onSearchChange.bind(this); this.isSearched=this.isSearched.bind(this); this.answerSubmitted=this.answerSubmitted.bind(this); 此.state={ 答复:[], 答复:",, 搜索词:“”, } } 答复(事件){ event.preventDefault(); const input=event.target.querySelector('input'); 常量值=input.value; const updatedList=this.state.answer; updatedList.push(值); this.setState({answer:updatedList}); } render(){ 返回( 搜索: 问答: 提交 {basicFormat} {this.state.answers.filter(this.isSearched(this.state.searchTerm)).map(函数(项){ 返回( {item} ) } ) } ); } },reactjs,Reactjs,为什么我不能在这里使用这个函数?错误:无法读取未定义的属性“answerSubmitted”。我不太清楚为什么会发生这种情况,我试着四处搜索,但我能找到的是,人们并没有像我这样绑定他们的方法。非常感谢您的帮助。您传递给map的函数将更改此上下文。改用箭头函数: constructor(props) { super(props); this.submitQA = this.submitQA.bind(this); this.onSearchChange = this.onSearchC

为什么我不能在这里使用这个函数?错误:无法读取未定义的属性“answerSubmitted”。我不太清楚为什么会发生这种情况,我试着四处搜索,但我能找到的是,人们并没有像我这样绑定他们的方法。非常感谢您的帮助。

您传递给
map
的函数将更改
上下文。改用箭头函数:

constructor(props) {
  super(props);
  this.submitQA = this.submitQA.bind(this);
  this.onSearchChange = this.onSearchChange.bind(this);
  this.isSearched = this.isSearched.bind(this);
  this.answerSubmitted = this.answerSubmitted.bind(this);
  this.state = {
    answers: [],
    answer: '',
    searchTerm: '',
  }
}

answerSubmitted(event) {
 event.preventDefault();
 const input = event.target.querySelector('input');
 const value = input.value;
 const updatedList = this.state.answer;
 updatedList.push(value);
 this.setState({ answer: updatedList });
}



 render() {
    return (
    <div className="App">
      <div className="center">

        <form >
          Search:  <input type="text" onChange={this.onSearchChange}  /><br/>
        </form>

        <form onSubmit={this.submitQA}>
          Q & A:
          <input type="text" placeholder=" Course/Q/A"/>
          <button type="submit"> Submit </button>
        </form>
          <span>{basicFormat}</span>
      </div>

{ this.state.answers.filter(this.isSearched(this.state.searchTerm)).map(function(item) {
return (
    <div>
      <form >
        <text> {item} </text>
        <input onSubmit={this.answerSubmitted} type="text" placeholder="answer the question"/>
      </form>
    </div>
)
  }
    )
      }
      </div>
    );
  }
}
{this.state.answers.filter(this.isSearched(this.state.searchTerm)).map((项)=>(
{item}
))}
箭头函数始终与定义它们的上下文具有相同的
。但是,其他函数会根据调用方式更改其

{this.state.answers.filter(this.isSearched(this.state.searchTerm)).map((item) => (
  <div>
    <form>
      <text> {item} </text>
      <input onSubmit={this.answerSubmitted} type="text" placeholder="answer the question"/>
    </form>
  </div>
))}