当我使用 useChange 从数字键盘设置值时无效的钩子调用



这是我的组件:

const pricePicker = ({
step,
precision,
input,
placeholder,
label,
theme,
props,
meta: { touched, error },
...rest
}) => {
/*In the FOLLOWING LINES from "function useChange(e)" to "return [value,change]"
is the error known as Invalid hook.
*/
function useChange(e){
const [value,setValue] = useState(0);
function change(event){
setValue(value => event.target.value);
}
return [value,change];
}
const handleBlur = (e) => {
if (e.target.value === '0') e.target.value = '0'
}

const handleKeypress = (e)  => {
const characterCode = e.key
if (characterCode === 'Backspace') return
const characterNumber = Number(characterCode)
if (characterNumber < 0) {
e.preventDefault()
}
}
const myTheme = {
fontFamily: 'Arial',
textAlign: 'center',
header: {
primaryColor: '#263238',
secondaryColor: '#f9f9f9',
highlightColor: '#FFC107',
backgroundColor: '#607D8B',
},
body: {
primaryColor: '#263238',
secondaryColor: '#32a5f2',
highlightColor: '#FFC107',
backgroundColor: '#f9f9f9',
},
panel: {
backgroundColor: '#CFD8DC'
}
};

return(
<div className='form-group'> 
<label forname={input.name}>{label}</label> <br />
<NumPad.Number
{...rest}
step={0.1}
precision={2}
placeholder={!input.value ? 'Please, type a number' : input.value}
selected={input.value ? new NumPad.Number(input.value) : null}
onKeyDown={(changedVal) => handleKeypress(changedVal)}
onBlur={(changedVal) => handleBlur(changedVal)}
onChange={(changedVal) => useChange(changedVal)}

className='form-control'

/>
<div className='text-danger' style={{ marginBottom: '20px' }}>
{touched && error}
</div>
</div>
);
};
export default pricePicker;

当我执行这个代码块时:

function useChange(e){
const [value,setValue] = useState(0);
function change(event){
setValue(value => event.target.value);
}
return [value,change];
}

我得到以下问题:

无效的挂钩调用。钩子只能在函数组件的主体内部调用。这可能是由于以下原因之一:1.React和渲染器的版本可能不匹配(例如React DOM(2.你可能违反了胡克规则3.在同一应用中,您可能有多个React副本

我试过各种方法,但似乎不可能。我从来没有用过钩子,以前我也发过类似的帖子,但并不成功。上一篇文章谈到useState在pricePicker内部。当执行以前的代码行时,函数既不是函数组件,也不是反作用钩子组件,如下所示:

const handleChange = (e) =>{
// in the following line is the error.
const[value, setValue] = useState(0);
}

我该如何解决这个问题?我需要修复它,但是怎么修复呢?我尝试了各种方法,但都没有成功。

有人知道我该如何解决这个问题吗?这很重要。

错误实际上很简单——钩子只能在功能组件的顶层使用。在您的具体示例中,不能在函数useChange内部使用useState

相反,做一些类似的事情:

const pricePicker = ({/*props go here*/
const [value,setValue] = useState(0);
// handler of input change
const onChange = e => setValue(e.target.value);
// the rest of the code can stay the same
return <div className='form-group'> 
<label forname={input.name}>{label}</label> <br />
<NumPad.Number
{...rest}
step={0.1}
precision={2}
placeholder={!input.value ? 'Please, type a number' : input.value}
selected={input.value ? new NumPad.Number(input.value) : null}
onKeyDown={(changedVal) => handleKeypress(changedVal)}
onBlur={(changedVal) => handleBlur(changedVal)}
onChange={onChange} /* Here's we use the onChange handler */
className='form-control'
/>
<div className='text-danger' style={{ marginBottom: '20px' }}>
{touched && error}
</div>
</div>;
}

最新更新