TypeError:this.props.sendKitchen不是函数……为什么?

时间:2019-05-18 20:20:05

标签: reactjs react-router

我知道这个问题经常被问到,但是我没有找到解决方案,我也不知道为什么会出现这个错误。 typeError在我的Order(子)组件中。而且我使用react和react-router。

这是我的孩子部分:

class Order extends Component{
    constructor(props){
        super(props);
        this.sendKitchen=this.sendKitchen.bind(this);
        this.state={
            name:'',
            choiceMenu:[],
            order:[],
            total:0,
            timeS:'',
            timeN:0
        }
    }
    sendKitchen(){

        let orderString=[];
        (this.state.order).forEach(food=>{orderString.push(food.name)});

        let date=new Date();
        let dateString=date.getHours()+':'+date.getMinutes();
        let dateNumber=date.getTime();

        let client={
            name:this.state.name,
            delivery: false,
            total: parseInt(this.state.total),
            timeS:dateString,
            timestamp:dateNumber,
            order: orderString
        };  
        this.props.toKitchen(client);
    }
    render(){
        return(

              <Button variant="success" onClick={this.sendKitchen}>ENVIAR A COCINA</Button>
        )}

这是我的父组件

class App extends Component{
  constructor(props){
    super(props);
    this.toKitchen=this.toKitchen.bind(this);
    this.state={
      clients:[]
    }
  }
  toKitchen(client){
    console.log(client);
  }

  render(){
    return (
      <div >

        <Order
          sendKitchen={this.toKitchen}/>

      </div>
    );
  }
}

我尝试了很多解决方案,但仍然存在相同的错误

编辑:我删除了react-router部分,我更改了传递道具的函数名称,但仍然无法正常工作。另外,我将该功能更改为箭头功能,但出现相同的错误,因此我删除了该更改。

1 个答案:

答案 0 :(得分:0)

真是一团糟-它没有任何意义-尝试了太多选择;)

<App/>通常呈现为html,而不是<Router/>

<Router/>应该在<App/>

this.props.sendKitchen-path='/' component={App} <App/> renders sendKitchen`道具是唯一可行的with路由器案例。对于“ / order”,此道具未定义。

此处没有redux,请删除标签。