[英]React props between class
I'm working on a react
project, and I'm using different classes.我正在做一个
react
项目,我正在使用不同的类。
I would bring a number value into a props for another class but it doesn't work.我会将一个数值带入另一个 class 的道具中,但它不起作用。
const [refreshRate, setRefreshRate] = useState(60);
function changeRefreshRate(e) {
setRefreshRate(e.target.value);
}
let list = [
{
id:'weather',
label:'Weather',
widget:<Weather refreshRate={refreshRate}/>, //not update
checked: false
}
];
console.log(refreshRate)
return(
<div>
<DragDropContext onDragEnd={handleOnDragEnd}>
<Droppable droppableId="widgets">
{(provided) => (
<ul className="widgets" {...provided.droppableProps} ref={provided.innerRef}>
{widgets.map(({id, label, widget, checked}, index) => {
return (
<Draggable key={id} draggableId={id} index={index}>
{(provided) => (
<li ref={provided.innerRef} {...provided.draggableProps} {...provided.dragHandleProps}>
<Checkbox id={id} label={label} onChange={_onBoxChecked} />
{checked ? widget : ""}
<div>
<input class="boxnbr" type='number' min='0' onChange={changeRefreshRate}></input>
In my Weather
class I receive 60 in any case.在我的
Weather
class 中,无论如何我都会收到 60。 Even if I change my RefreshRate
it is not sent.即使我更改了
RefreshRate
,它也不会发送。 But, in my console log of this class, I can have the good input.但是,在我的这个 class 的控制台日志中,我可以有很好的输入。 So, my
setRefreshRate
works.所以,我的
setRefreshRate
有效。 How is it possible?这怎么可能?
You can pass your initial state as first argument to useState like this:您可以将初始 state 作为第一个参数传递给 useState,如下所示:
const Weather = (props) => {
const [refreshRate, setRefreshRate] = useState(props.parentRefreshRate);
...
}
where parentRefreshRate is the prop value which you would send from the parent class.其中 parentRefreshRate 是您将从父 class 发送的道具值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.