传递新道具时,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 initiallyfalse
and then updated in the parent component via a button. I'm checking the component state via theconsole.log
statement in the render function. When the component is first initialized, it logsfalse false
as expected but when the isVisible is updated, it returnsfalse 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
最满意答案
您设置了一个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
相关问答
更多-
短地址模式和长寻址模式之间有什么区别(What is the difference between short adressing mode and long addressing mode)[2023-08-13]
短寻址模式使用相对地址,计算为(某些寄存器+一些小的常数),通常允许小/少/快速指令,但只能寻址小范围的存储器; 长寻址模式使用绝对地址,通常需要大/多/慢指令,但可以访问任何内存。 我将举例说明ARM代码,但这可能适用于(在一般意义上)许多其他处理器。 每个ARM指令(忽略Thumb)都是32位长,为了这个例子,我们假装ARM可以访问32位地址空间。 必须对每条指令进行解码,主要是将这32位分解为各种字段 - 这些位中的一些必须用于存储指令的类型。 因此,希望您能够清楚地知道在一条指令中不能将任意地址加载 ... -
间接/索引寻址模式(Indirect/Indexed Addressing Mode)[2024-01-10]
不确定你正在讨论哪种架构,所以我将尽可能地解释一下(基于更具体的架构的经验,以及对你发布的图形中显示的东西的调查分析)。 立即模式意味着使用立即值,因此load r2, #800类的东西会将立即值800置于寄存器2中。 直接意味着直接内存访问,因此像load r2, 800这样的东西load r2, 800来自内存地址800的值 ,该值为900。 间接意味着间接存储器访问,因此诸如load r2, (800)类的东西从存储器地址800处的存储器地址加载值 load r2, (800)处的存储器地址是900并 ... -
保护模式寻址(Protected Mode Addressing)[2024-03-14]
您设置了一个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 ... -
装配PC相对寻址模式(Assembly PC Relative Addressing Mode)[2022-06-18]
向左移位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 ...
-
我们如何在Linux 2.6中从保护模式切换到实模式?(How do we shift from protected mode to real mode in Linux 2.6?)[2022-09-11]
你不。 内核在真实模式下根本无法工作,也不能(以合理的方式)制作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 ... -
有关寻址模式的查询(Query regarding addressing modes)[2023-07-08]
在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上,这将需要一个副 ...
-
间接寻址模式(Indirect Addressing Mode)[2022-11-21]
出于问题的目的,假设地址存储在内存位置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.