Javascript 为什么我不能在React中获取输入值?

Javascript 为什么我不能在React中获取输入值?,javascript,reactjs,redux,antd,Javascript,Reactjs,Redux,Antd,我可以添加e.target.value从输入中获取值,但我不知道为什么handleChange()无法正确获取输入值,它将获得title:SyntheticBaseEvent,在选项部分,它将接收一个正确的值,即0,1,2,3。我正在使用AntdUI。我的代码中是否遗漏了某些内容,请帮助我解决它 handleChange = (type, v) => { this.data.Schedule[type] = v; }; <Input pla

我可以添加
e.target.value
从输入中获取值,但我不知道为什么
handleChange()
无法正确获取输入值,它将获得
title:SyntheticBaseEvent
,在选项部分,它将接收一个正确的值,即0,1,2,3。我正在使用AntdUI。我的代码中是否遗漏了某些内容,请帮助我解决它

  handleChange = (type, v) => {
    this.data.Schedule[type] = v;
  };
      <Input
        placeholder="Please Type Your Title"
        className="todo-ipt"
        value={this.state.curSchedule.title}
        onChange={this.handleChange.bind(this, 'title')}
      />
    </div>
    <Select
      placeholder="Please Type Schedule Type"
      type="date"
      className="todo-select"
      defaultValue={this.state.curSchedule.type}
      onChange={this.handleChange.bind(this, 'type')}
    >
      <Option value="0">Daily</Option>
      <Option value="1">Weekly</Option>
      <Option value="2">Monthly</Option>
      <Option value="3">Quarterly</Option>
    </Select>
handleChange=(类型,v)=>{
this.data.Schedule[type]=v;
};
每日的
周报
月刊
每季的
antd
onChange方法传递所选值,但不转发事件。(其中,
组件不传递值,但转发事件。)

要使用
bind()
使用单个处理程序实现这一点,您需要从antd中解释这种不一致性

const handleChange = (type, v, e) => {
  const value = v !== null ? v : e.target.value; // handle the inconsistent antd arguments
  setCurSchedule(prev => ({ ...prev, [type]: value }))
};

...

<Input
...
  onChange={handleChange.bind(null, 'title', null)}
  // 'v' will be null, 'e' will be the implicitly passed event.
/>

<Select
  ...
  onChange={handleChange.bind(null, 'type')} 
  // 'v' implicitly passed, 'e' will be the selected option (not the event)
>
consthandlechange=(type,v,e)=>{
const value=v!==null?v:e.target.value;//处理不一致的antd参数
SetCursSchedule(prev=>({…prev[type]:value}))
};
...
const{Input,Select}=antd;
常量应用=()=>{
const[curSchedule,setCurSchedule]=React.useState({type:'Daily'});
控制台日志(curSchedule);
常量句柄更改=(类型,v,e)=>{
console.clear();
log('type:',type,'v:',v,'e:',e.target);
const value=v!==null?v:e.target.value;//处理不一致的antd参数
SetCursSchedule(prev=>({…prev[type]:value}))
};
返回(
每日的
周报
月刊
每季的
)
}
ReactDOM.render(
,
document.getElementById(“react”)
);


您正在对其
无法绑定的箭头函数调用
bind
<然后将code>title
添加到内部传递的参数(事件)前面,因此
type
是'title',
v
event
保持不变(因此保留了声明箭头函数的外部范围)。请参阅:,感谢pilchard,在选项部分,它将收到一个正确的值,即0,1,2,3。正如您提到的,它也应该传递事件吗?antd onChange方法传递所选的值,但它似乎没有转发事件。(其中组件不转发值,但传递事件。)要使用单个处理程序进行此操作,您需要从antd中解释此不一致性,但编写单独的输入并选择处理程序更为清晰。