[英]React Hook useState returns undefined for props value but props value appears on textbox value
So I have this code wherein the value of a textbox is from a prop.所以我有这段代码,其中文本框的值来自道具。 When i put the "props.completeAddress" directly to the textbox value, the value appears without a problem.
当我将“props.completeAddress”直接放入文本框值时,该值出现没有问题。 But I have to make it editable.
但我必须让它可编辑。 So I have a const[inputAddress, setInputAddress] and in the useState I put the props.completeAddress, then used "inputAddress" for the value of a textbox.
所以我有一个 const[inputAddress, setInputAddress] 并在 useState 中放置了 props.completeAddress,然后使用“inputAddress”作为文本框的值。 Ideally the value should appear but it returns "undefined".
理想情况下,该值应该出现,但它返回“未定义”。
Photo for reference: what appears when i directly put "props.completeAddress" for the textbox value供参考的照片:当我直接将“props.completeAddress”作为文本框值时会出现什么
function BarangayInput(props) {
const [inputAddress, setInputAddress] = useState(
props.completeAddress
);
const [showList, setShowList] = useState(false);
const [barangayList, setBarangayList] = useState([]);
function onChange(e) {
props.onChangeHandler(e, "barangay");
getBarangayAPI();
}
helper.log("[Barangay Input]: render");
helper.log("[Barangay Input]: render", barangayList);
return (
<div style={{ margin: "0" }} className={style.barangay}>
<input
style={{ width: "100%" }}
className={style.inputstyle + " " + style.barangayinput}
disabled={!props.isEditable}
onChange={onChange}
value={inputAddress}
id={8}
placeholder="Ex: Santolan, Pasig City, 1610"
//value={props.partnerProfile.email_address}
/>
{showList ? <div className={style.listContainer}>{list}</div> : null}
</div>
);
}
const mapStateToProps = (state) => {
return {
barangay: state.profileAndDetails.barangay,
completeAddress:
state.profileAndDetails.partnerProfileAndDetails.barangay +
" " +
state.profileAndDetails.partnerProfileAndDetails.city +
" " +
state.profileAndDetails.partnerProfileAndDetails.province +
" " +
state.profileAndDetails.partnerProfileAndDetails.zip_code,
barangay: state.profileAndDetails.partnerProfileAndDetails.barangay,
};
};
I see no issue other than the onChange
handler isn't updating the local state.除了
onChange
处理程序没有更新本地 state 之外,我没有看到任何问题。
function BarangayInput(props) {
const [inputAddress, setInputAddress] = useState(props.completeAddress);
...
function onChange(e) {
props.onChangeHandler(e, "barangay");
getBarangayAPI();
setInputAddress(e.target.value); // <-- update state
}
...
return (
<div style={{ margin: "0" }} className={style.barangay}>
<input
style={{ width: "100%" }}
className={style.inputstyle + " " + style.barangayinput}
disabled={!props.isEditable}
onChange={onChange}
value={inputAddress}
id={8}
placeholder="Ex: Santolan, Pasig City, 1610"
/>
...
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.