传递回调函数的问题,写错props.me is not a function,我只能展示部分代码
The problem with passing callback functions, writes an error props.me is not a function, I can only show part of the code
有一个parent和一个child(组件)我想获取从child到parent的值,我把函数扔了自己进入组件,在 child 我写了 return 目标值来自 input
parent
const [m, setM] = useState("")
const me = (m) => {
setM(m)
console.log(m)
}
const [dey, setD] = useState("")
const de = (dey) => {
setD(dey)
}
const [y, setY] = useState("")
const ye = (y) => {
setY(y)
}
其companent connect in jsx
<CustomSelect
className="flex-1"
btnClass="inp"
checkedOpt={dey}
options={d}
de={de}
/>
<CustomSelect
className="flex-1 ms-2 ms-xxl-4"
btnClass="inp"
checkedOpt={m}
options={month}
me={me}
/>
<CustomSelect
className="flex-1 ms-2 ms-xxl-4"
btnClass="inp"
checkedOpt={y}
options={years}
ye={ye}
/>
child
const handleChange = (e) => {
props.de(e.target.value);
props.me(e.target.value);
props.ye(e.target.value);
}
jsx
<input
type="radio"
name="type"
value={item}
checked={(item === props.checkedOpt)}
onChange={handleChange}
/>
由于 props.me 并未传递给每个组件,您只需检查它是否作为函数传递:
const handleChange = (e) => {
props.de?.(e.target.value);
props.me?.(e.target.value);
props.ye?.(e.target.value);
}
有一个parent和一个child(组件)我想获取从child到parent的值,我把函数扔了自己进入组件,在 child 我写了 return 目标值来自 input
parent
const [m, setM] = useState("")
const me = (m) => {
setM(m)
console.log(m)
}
const [dey, setD] = useState("")
const de = (dey) => {
setD(dey)
}
const [y, setY] = useState("")
const ye = (y) => {
setY(y)
}
其companent connect in jsx
<CustomSelect
className="flex-1"
btnClass="inp"
checkedOpt={dey}
options={d}
de={de}
/>
<CustomSelect
className="flex-1 ms-2 ms-xxl-4"
btnClass="inp"
checkedOpt={m}
options={month}
me={me}
/>
<CustomSelect
className="flex-1 ms-2 ms-xxl-4"
btnClass="inp"
checkedOpt={y}
options={years}
ye={ye}
/>
child
const handleChange = (e) => {
props.de(e.target.value);
props.me(e.target.value);
props.ye(e.target.value);
}
jsx
<input
type="radio"
name="type"
value={item}
checked={(item === props.checkedOpt)}
onChange={handleChange}
/>
由于 props.me 并未传递给每个组件,您只需检查它是否作为函数传递:
const handleChange = (e) => {
props.de?.(e.target.value);
props.me?.(e.target.value);
props.ye?.(e.target.value);
}