用两个组件来响应handleChange错误

我想从第二个组件调用事件handleChange

主要组成部分:

  const [state, setState] = useState({ step: 1, saleDate: new Date(), customer: '' });

  function nextStep() {
    const { step } = state;
    setState({
      step: step + 1
    });
  };

  function prevStep() {
    const { step } = state;
    setState({
      step: step - 1
    });
  };

  function handleChange(e) {
    setState(e.target.value);
  };


  const { step } = state;

  switch (step) {
    case 1:
      return <SaleStepDate
        nextStep={nextStep}
        handleChange={handleChange}
        values={state} />
  }
}

第二部分,这是错误

const SaleStepDate = (props) => {

  function continueStep(e) {
    e.preventDefault();
    props.nextStep();
  };

  const { values, handleChange } = props;

  return (
      <TextField
         placeholder="Nombre cliente"
         label="Cliente"
         name="customer"
         margin="normal"
         fullWidth="true"
         onChange={handleChange('customer')}
       />
  )
}

错误发生在textField事件中的第二个组件onChange = {handleChange('customer')}

TypeError:无法读取未定义的属性“值”

请帮忙

评论
  • Kennedy
    Kennedy 回复

    分配给onChange时,您正在调用handleChange函数。您需要将其更改为:

    <TextField
             placeholder="Nombre cliente"
             label="Cliente"
             name="customer"
             margin="normal"
             fullWidth="true"
             onChange={handleChange}
           />