首页 \ 问答 \ 传递新道具时,React子组件状态不会更新(React child component state not updating when new props are passed)

传递新道具时,React子组件状态不会更新(React child component state not updating when new props are passed)

我正在渲染一个自定义模态组件,该组件基于从父组件传入的props显示。 prop isVisible最初为false ,然后通过按钮在父组件中更新。 我正在通过render函数中的console.log语句检查组件状态。 首次初始化组件时,它会按预期记录false false ,但是当更新isVisible时,它返回false true 。 为什么州不用道具更新?

class MyModal extends React.Component {
  constructor(props) {
    super(props);
      this.state = {
        createModalVisible:props.isVisible,
      };

   setCreateModalVisible = (visible) => {
     this.setState({createModalVisible:visible});
   }

   componentWillMount(){
     this.setCreateModalVisible(this.props.isVisible);
   }

  }
  render() {
    console.log(this.state.createModalVisible,this.props.isVisible);

    return (//modal stuff)
  }
}
export default MyModal

我知道这可能是相当基本的组件生命周期的东西,但我无法从文档中找到它,而且对于React来说还是比较新的。


I'm rendering a custom modal component that is displayed based on props passed in from a parent component. The prop isVisible is initially false and then updated in the parent component via a button. I'm checking the component state via the console.log statement in the render function. When the component is first initialized, it logs false false as expected but when the isVisible is updated, it returns false true. Why is the state not updating with the props?

class MyModal extends React.Component {
  constructor(props) {
    super(props);
      this.state = {
        createModalVisible:props.isVisible,
      };

   setCreateModalVisible = (visible) => {
     this.setState({createModalVisible:visible});
   }

   componentWillMount(){
     this.setCreateModalVisible(this.props.isVisible);
   }

  }
  render() {
    console.log(this.state.createModalVisible,this.props.isVisible);

    return (//modal stuff)
  }
}
export default MyModal

I get that this is probably pretty basic component lifecycle stuff but I couldn't figure it out from the docs and am fairly new to React.


原文:https://stackoverflow.com/questions/45528800
更新时间:2023-04-17 17:04

最满意答案

您设置了一个GDT,它具有代码的32位描述符和数据的32位描述符。 在保护模式下,寄存器CS / DS / ES / SS / FS / GS不再是在实模式下看到的段寄存器。 在保护模式下,它们包含一个指向GDT(或LDT)中的条目的选择器。 此代码加载数据寄存器:

mov ax, DATA_DESC       ; set data segments to data selector (0x10)
mov ds, ax
mov ss, ax
mov es, ax

您不能像这样设置CS寄存器。 FAR jmp可以设置所需的代码段选择器,并在一条指令中设置偏移量。 这就是这个指令的作用:

jmp CODE_DESC:Stage3

您的GDT设置了代码和数据描述符,这些描述符是4GB平面内存模型,其中基数为0x00000000,限制为0xffffffff。 这意味着一旦处于保护模式并且您正在使用指向这些描述符的选择器,您可以直接从0x00000000到0xffffffff访问所有内存。 这意味着你需要做的就是jmp 0x100000跳转到内存地址0x100000。 在您的代码中,您将不仅要使用0x100000作为跳转位置,还要使用内存副本用作其基础的内存位置。

考虑到这一点,简单的解决方法是改变:

IMAGE_PMODE_BASE equ 0x1000

至:

IMAGE_PMODE_BASE equ 0x100000

You set up a GDT that has a 32-bit Descriptor for code and 32-bit descriptor for data. In protected mode the registers CS/DS/ES/SS/FS/GS are no longer segment registers in the sense they were seen in real mode. In protected mode they contain a selector that points to an entry in the GDT (or an LDT). This code loads the data registers:

mov ax, DATA_DESC       ; set data segments to data selector (0x10)
mov ds, ax
mov ss, ax
mov es, ax

You can't set the CS register like this. A FAR jmp can set both the code segment selector you want and sets the offset in one instruction. That is what this instruction does:

jmp CODE_DESC:Stage3

Your GDT is set up with code and data descriptors that are a 4gb flat memory model where the base is 0x00000000 and limit is 0xffffffff. This means once in protected mode and you are using selectors that point at these descriptors you can access all memory directly from 0x00000000 to 0xffffffff. This means that all you need to do is jmp 0x100000 to jump to memory address 0x100000. In your code you'll want to use 0x100000 as not only the place to jump to but also the memory location your memory copy uses as its base.

With that in mind, the simple fix is to change:

IMAGE_PMODE_BASE equ 0x1000

to:

IMAGE_PMODE_BASE equ 0x100000

相关问答

更多
  • 短寻址模式使用相对地址,计算为(某些寄存器+一些小的常数),通常允许小/少/快速指令,但只能寻址小范围的存储器; 长寻址模式使用绝对地址,通常需要大/多/慢指令,但可以访问任何内存。 我将举例说明ARM代码,但这可能适用于(在一般意义上)许多其他处理器。 每个ARM指令(忽略Thumb)都是32位长,为了这个例子,我们假装ARM可以访问32位地址空间。 必须对每条指令进行解码,主要是将这32位分解为各种字段 - 这些位中的一些必须用于存储指令的类型。 因此,希望您能够清楚地知道在一条指令中不能将任意地址加载 ...
  • 不确定你正在讨论哪种架构,所以我将尽可能地解释一下(基于更具体的架构的经验,以及对你发布的图形中显示的东西的调查分析)。 立即模式意味着使用立即值,因此load r2, #800类的东西会将立即值800置于寄存器2中。 直接意味着直接内存访问,因此像load r2, 800这样的东西load r2, 800来自内存地址800的值 ,该值为900。 间接意味着间接存储器访问,因此诸如load r2, (800)类的东西从存储器地址800处的存储器地址加载值 load r2, (800)处的存储器地址是900并 ...
  • 您设置了一个GDT,它具有代码的32位描述符和数据的32位描述符。 在保护模式下,寄存器CS / DS / ES / SS / FS / GS不再是在实模式下看到的段寄存器。 在保护模式下,它们包含一个指向GDT(或LDT)中的条目的选择器。 此代码加载数据寄存器: mov ax, DATA_DESC ; set data segments to data selector (0x10) mov ds, ax mov ss, ax mov es, ax 您不能像这样设置CS寄存器。 FAR j ...
  • 向左移位n位与将数字乘以2 n相同 。 向左移动2位乘以4。 如果您的分支偏移量向左移动2,则意味着您的分支偏移操作数是整个指令单位,而不是字节。 所以带有8个操作数的分支指令意味着跳转8个指令,即32个字节。 MIPS乘以4,因为指令总是32位。 32位是4个字节。 MIPS指令保证从一个可被4整除的地址开始。这意味着PC的低两位保证始终为零,因此所有分支偏移都保证在低两位具有00。 因此,在分支指令中存储低位是没有意义的。 MIPS设计人员正在尝试最大化分支指令可以达到的范围。 PC表示“程序计数器”。 ...
  • 寻址模式在前几页的指令集中进行了说明 。 查看指令表以查看用于确定寻址模式的指令的操作数。 该模式由汇编指令的参数指示。 例如, LD有几种不同的操作方式,具体取决于参数。 其中一个是LD Rd,X其中d是寄存器的编号,例如LD R3,X 。 LD R3,-Z做其他事情(尽管两者都是间接寻址)。 RJMP k是相对寻址,跳转到PC + k + 1。 ADD Rr,Rd是直接寄存器寻址。 结果包含在Rd中。 The addressing modes are explained in the instructi ...
  • 你不。 内核在真实模式下根本无法工作,也不能(以合理的方式)制作BIOS,因此无论如何您都无法做任何事情。 您可以看看DosEMU如何使用v86模式来运行您的“实模式”代码,但仅此而已。 You do not. The kernel cannot function at all in real mode, nor can bios calls be (sensibly) made, so you wouldn't be able to do anything anyway. You might look a ...
  • 在MIPS中,地址总是有两部分,一部分是常量(16位符号扩展),另一部分是来自寄存器的值。 索引模式和位移模式之间的区别在于:哪些部分用于以下目的:在索引模式中,常量部分是指某物的基础,寄存器是物体的偏移/索引,而对于位移模式,寄存器指向事物的基础和常数是对事物的偏移。 所以这: 两者似乎都相同,因为它们都有偏移量和一个基址寄存器,而有效地址是通过将寄存器的内容与偏移量相加来计算的。 是真的。 它们是相同的,它也与间接和绝对模式一样,它们只是语法结构:地址计算中仍然有两个部分,但寄存器可以是$ 0或常量可以 ...
  • 我不明白这种寻址模式的实际需要。 为什么我们不能通过直接寻址来做到这一点? 您可以; MIPS只有一种寻址模式,编译器仍可以为它生成代码。 但有时它必须使用额外的shift + add指令来计算地址(如果它不只是循环遍历数组)。 寻址模式的目的是保存指令并保存寄存器,特别是在像x86这样的2操作数指令集中,其中add eax, ecx用结果覆盖eax ( eax += ecx ),这与MIPS或其他3指令ISA addu $t2, $t1, $t0确实t2 = t1 + t0 。 在x86上,这将需要一个副 ...
  • 出于问题的目的,假设地址存储在内存位置10.毕竟,这就是真正的CPU所做的事情。 如果地址结果无效,CPU可能会发送信号或终止违规进程。 假设内存包含: 10: 100 20: 200 100: 1000 注册#10包含: 20 答案是: 10:操作数包含在指令本身中。 100 1000 20:寄存器本身包含操作数。 寄存器号在指令中编码。 200:寄存器包含操作数的地址。 For the purposes of the question, assume that an address is stored ...
  • 注册访问速度最快。 但是,如果您正在访问的内存数据已经存在于CPU的数据缓存中,则内存访问速度可能会更快。 Register accesses are the fastest. However, memory accesses can be as fast if the memory data that you're accessing is already in the CPU's data cache.

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。