Eri*_*ell 4 reactjs react-state-management react-hooks
我正在使用 React 构建一个应用程序,其中两个组件应该能够更改彼此的状态:
组件 A -> 组件 B
组件 B -> 组件 A
组件 A 是一组按钮,组件 B 是一个输入元素。
我设法让它只以一种方式工作,A -> B 或只是 B -> A,但不能让它同时工作。它可以部分使用 useEffect 钩子,但有错误,我认为这确实是愚蠢的想法。
我读过很多关于 React 不能以这种方式工作的文章,但是有什么迂回方式可以使其工作吗?我的应用程序确实需要这种 2 路数据绑定。
谢谢你的帮助!
按钮的状态位于自定义上下文挂钩 (useStateContext) 中的数字变量中,作为数组。
import { useStateContext } from "components/StateProvider/Context";
import { useState, useEffect } from "react";
import { baseConvert } from "utility/baseConvert";
const NumberInput = () => {
const [ { digits, baseIn, baseOut }, dispatch ] = useStateContext();
const convertedValue = baseConvert({
digits,
baseIn,
baseOut
});
const [ inputValue, setInputValue ] = useState(convertedValue);
/* useEffect(() => {
setInputValue(convertedValue)
}, [ digits, baseIn, baseOut ]) */
const regex = /^[A-Z\d]+$/;
const handleInput = ({ target: { value }}) => {
if (value === "") setInputValue(0);
console.log(value);
if (regex.test(value)) {
setInputValue(value);
dispatch({
type: "setDigits",
digits: baseConvert({
digits: value.split("").map(Number),
baseIn: baseOut,
baseOut: baseIn
})
})
}
};
return (
<input type="text" onChange={handleInput} value={inputValue} />
);
};
export default NumberInput;
Run Code Online (Sandbox Code Playgroud)
组件不应直接操纵其他组件的状态。如果您需要共享数据,请将状态传递给父组件并将回调传递给可以更改状态的子组件。
例如:
function ParentComponent() {
const [currentVal, setCurrentVal] = useState(0);
return
<>
<Child1 value={currentVal} onChange={setCurrentVal}/> // you might also pass a function that does some other logic and then calls setCurrentVal
<Child2 value={currentVal} onChange={setCurrentVal}/>
</>
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12040 次 |
| 最近记录: |