Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 为什么赢了';当状态更新时,是否填充响应模式输入值?_Reactjs_React Hooks_Use Effect_Use State - Fatal编程技术网

Reactjs 为什么赢了';当状态更新时,是否填充响应模式输入值?

Reactjs 为什么赢了';当状态更新时,是否填充响应模式输入值?,reactjs,react-hooks,use-effect,use-state,Reactjs,React Hooks,Use Effect,Use State,我有一个模式,在点击按钮时触发,需要填充一个用户可以编辑的输入值。单击按钮时,模式状态将更新,但输入: a。不将更新状态反映为其值(当我使用“defaultValue”时会发生这种情况) --或-- b、 不允许用户编辑输入中的值(当我使用“值”时会发生这种情况) 如何用当前状态填充输入,并对其进行编辑?是否应更改输入值设置为触发模式重新渲染的状态 function EditModal(props) { const [value, setValue] = useState(props.m

我有一个模式,在点击按钮时触发,需要填充一个用户可以编辑的输入值。单击按钮时,模式状态将更新,但输入:

a。不将更新状态反映为其值(当我使用“defaultValue”时会发生这种情况) --或--

b、 不允许用户编辑输入中的值(当我使用“值”时会发生这种情况)

如何用当前状态填充输入,并对其进行编辑?是否应更改输入值设置为触发模式重新渲染的状态

function EditModal(props) {
    const [value, setValue] = useState(props.modalVal);
    const[hidden, setHidden] = useState(true)
    const[initialRender, setInitialRender] = useState(true)
    useEffect(()=>{
        setInitialRender(false)
    },[])
    useEffect(() => {
        setValue(props.modalVal)// <-- why doesn't this update input value?
    }, [props.modalVal]);
    useEffect(()=>{
        if(!initialRender){
            setHidden(false)
        }
    },[value])
    return (
        <Modal hidden={hidden} >
            <Text>Edit Item</Text>
            <form action="submit" onSubmit={props.submit}>
                <Input onChange={props.handleChange} defaultValue={value} />
                <Button>OK</Button>
            </form>
        </Modal>
    )
}
功能编辑模式(道具){
const[value,setValue]=使用状态(props.modalVal);
const[hidden,setHidden]=useState(true)
常量[initialRender,setInitialRender]=useState(真)
useffect(()=>{
setInitialRender(假)
},[])
useffect(()=>{

setValue(props.modalVal)/如果使用
value
设置值,则当用户编辑该值时,需要确保指定的
onChange
函数更新该值,以便在下次渲染时更新该值

看起来您有一个值(
props.modalVal
)应该用作
value
(注意,您不需要
useffect
useState
,因为更改道具会导致重新渲染)因此,您需要确保
props.handleChange
changes
modalValue

大概是这样的:

<Input onChange={props.handleChange} value={props.modalValue} />

在父组件中:

const ParentComponent = ()=> {
  const [modalValue, setModalValue] = useState('initialValue');

  const handleChange = (event) => {
    setModalValue(event.currentTarget.value);
  };

  return (
    <EditModal handleChange={handleChange} modalValue={modalValue} />
  )
}
constparentcomponent=()=>{
const[modalValue,setModalValue]=useState('initialValue');
常量handleChange=(事件)=>{
setModalValue(event.currentTarget.value);
};
返回(
)
}

您很可能应该使用
而不是
默认值
我不知道
输入
的实现,但是在正常的
输入
中,
默认值
只使用一次,不会反映更新。您是否能够记录并查看
道具。modalVal
的更新是否正确真的吗?这里的简单答案是不要从道具镜像状态。只需使用
value={props.modalVal}
。我可以发誓我已经试过了!!不管怎样,它成功了。谢谢!!通常当我在尝试新事物时,我会先尝试正确的方法,不会注意到输入错误或一些小错误,因此它不起作用。然后我尝试其他事情3小时/天,直到我意识到我一开始是正确的。。!