reactjs - 未捕获的 TypeError : props. handleRemoveOption 不是函数

标签 reactjs

我正在开发我的 React 应用程序,专门使用我的 handleRemoveOption 函数一次删除一项:

  handleRemoveOption(optionToRemove){
    this.setState((prevState) => ({
        options: prevState.options.filter((option) => optionToRemove !== option)
      }));
  }

这是我的完整代码:

class Box extends React.Component{
  constructor(props){
      super(props);
      this.state = {
          options: ['one', 'two', 'three']
      }
      this.handleRemoveAllOptions = this.handleRemoveAllOptions.bind(this);
      this.handleDecision = this.handleDecision.bind(this);
      this.handleAddOption = this.handleAddOption.bind(this);
      this.handleRemoveOption = this.handleRemoveOption(this);
  }
  handleRemoveAllOptions(){
      this.setState({
          options: []
      });
  }

  handleDecision(){
      const randomNum = Math.floor(Math.random() * this.state.options.length);
      const option = this.state.options[randomNum];
      alert(option);
  }

  handleAddOption(option){
      this.setState((prevState) => ({
          options: prevState.options.concat(option)
      }));
  }

  handleRemoveOption(optionToRemove){
    this.setState((prevState) => ({
        options: prevState.options.filter((option) => optionToRemove !== option)
      }));
  }

  render(){
    const title = 'Indecision app';
    const subtitle = 'Put your life..';
      return(
          <div>
             <Header 
               title={title} 
               subtitle={subtitle} 
             />
             <Action 
               handleDecision={this.handleDecision}
               hasOptions={this.state.options.length === 0}
             />
             <Options 
               options={this.state.options} 
               hasOptions={this.state.options.length === 0}
               handleRemoveAllOptions={this.handleRemoveAllOptions}
               handleRemoveOption={this.handleRemoveOption}
            />
             <AddOption handleAddOption={this.handleAddOption} />
          </div>
      );
  }
}

const Header = (props) => {
    return(
        <div>
            <h1>{props.title}</h1>
            <h3>{props.subtitle}</h3>
        </div>
    );
};


const Action = (props) => {
  return(
     <div>
        <button 
          onClick={props.handleDecision}
          disabled={props.hasOptions}>
          Decide for me
        </button>
     </div>
  );
};

const Options = (props) => {
    return(
      <div>
          <button 
            onClick={props.handleRemoveAllOptions}
            disabled={props.hasOptions}>
            Remove All
          </button>
         <ol>
            { props.options.map((option) => (
              <Option 
                key={option} 
                optionText={option}
                handleRemoveOption={props.handleRemoveOption}
            /> 
            ))
            }
        </ol>
      </div>
    );
};

const Option = (props) => {
    return (
        <div>
                <li>
                    <span>{ props.optionText }</span>
                    <button 
                      onClick={(e) => {
                        props.handleRemoveOption(props.optionText);
                      }}>
                      Remove Option
                    </button>
                </li>
        </div>
    );
};

class AddOption extends React.Component{
   constructor(props){
       super(props);
       this.handleAddOption = this.handleAddOption.bind(this);
   }

    handleAddOption(e){
        e.preventDefault();
        const option = e.target.option.value.trim();
        this.props.handleAddOption(option);
    }
    render(){
        return(
            <div>
                <form onSubmit={this.handleAddOption}>
                    <input type="text" name="option" />
                    <button>Add Option</button>
                </form>
            </div>
        );
    }
}


ReactDOM.render(<Box />, document.getElementById('app'))

当我单击每个项目的单个按钮时,它总是显示Uncaught TypeError: props.handleRemoveOption is not a function

我在这里做错了什么?

最佳答案

Box 组件的 构造函数 中,所有函数都绑定(bind)到 this,但 handleRemoveOption 不是.

注意缺少的 this.handleRemoveOption.**bind**(this)

将第 13 行编辑为

    this.handleRemoveOption = this.handleRemoveOption.bind(this);

将解决您的问题!

关于reactjs - 未捕获的 TypeError : props. handleRemoveOption 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61866965/

相关文章:

reactjs - 如何测试其中包含 api 请求的 React 函数?

javascript - 如何在 react 中渲染一些可变的选择?

javascript - aws DynamoDB getItem InvalidParameterType 错误

javascript - 作为对象的一部分发送时无法读取 response.json()

reactjs - 如何在 react-bootstrap 中使用多选下拉菜单

javascript - 在离线(无网络)模式下显示 SVG

reactjs - React js - HOC 和装饰器之间有什么区别

javascript - 编写分支渲染的 ReactJS 组件

reactjs - 如何将 Redux DevTools 扩展添加到我的 React-Redux 商店?

javascript - 通过 firebase 更新 React Native 复选框的值