local Q=require(script.Parent.assign)local R=require(script.Parent.ComponentLifecyclePhase)local w=require(script.Parent.Type)local S=require(script.Parent.Symbol)local u=require(script.Parent.invalidSetStateMessages)local l=require(script.Parent.internalAssert)local h=(require(script.Parent.GlobalConfig)).get()local o=100 local V=S.named("\073\110\116\101\114\110\097\108\068\097\116\097")local Z="\084\104\101 \099\111\109\112\111\110\101\110\116 \037\113 \105\115 \109\105\115\115\105\110\103 \116\104\101 \096\114\101\110\100\101\114\096 \109\101\116\104\111\100\046\n\096\114\101\110\100\101\114\096 \109\117\115\116 \098\101 \100\101\102\105\110\101\100 \119\104\101\110 \099\114\101\097\116\105\110\103 \097 \082\111\097\099\116 \099\111\109\112\111\110\101\110\116\033"local s="\084\104\101 \099\111\109\112\111\110\101\110\116 \037\113 \104\097\115 \114\101\097\099\104\101\100 \116\104\101 \115\101\116\083\116\097\116\101 \117\112\100\097\116\101 \114\101\099\117\114\115\105\111\110 \108\105\109\105\116\046\n\087\104\101\110 \117\115\105\110\103 \096\115\101\116\083\116\097\116\101\096 \105\110 \096\100\105\100\085\112\100\097\116\101\096\044 \109\097\107\101 \115\117\114\101 \116\104\097\116 \105\116 \119\111\110\'\116 \114\101\112\101\097\116 \105\110\102\105\110\105\116\101\108\121\033"local B={}function B.__tostring(Q)return Q.__componentName end local y={}setmetatable(y,B)y[w]=w.StatefulComponentClass y.__index=y y.__componentName="\067\111\109\112\111\110\101\110\116"function y.extend(R,Q)if h.typeChecks then assert(w.of(R)==w.StatefulComponentClass,"\073\110\118\097\108\105\100 \096\115\101\108\102\096 \097\114\103\117\109\101\110\116 \116\111 \096\101\120\116\101\110\100\096\046")assert(typeof(Q)=="\115\116\114\105\110\103","\067\111\109\112\111\110\101\110\116 \099\108\097\115\115 \110\097\109\101 \109\117\115\116 \098\101 \097 \115\116\114\105\110\103")end local S={}for Q,R in pairs(R)do if Q~="\101\120\116\101\110\100"then S[Q]=R end end S[w]=w.StatefulComponentClass S.__index=S S.__componentName=Q setmetatable(S,B)return S end function y.__getDerivedState(S,Q,R)if h.internalTypeChecks then l(w.of(S)==w.StatefulComponentInstance,"\073\110\118\097\108\105\100 \117\115\101 \111\102 \096\095\095\103\101\116\068\101\114\105\118\101\100\083\116\097\116\101\096")end local u=S[V]local o=u.componentClass if o.getDerivedStateFromProps~=nil then local w=o.getDerivedStateFromProps(Q,R)if w~=nil then if h.typeChecks then assert(typeof(w)=="\116\097\098\108\101","\103\101\116\068\101\114\105\118\101\100\083\116\097\116\101\070\114\111\109\080\114\111\112\115 \109\117\115\116 \114\101\116\117\114\110 \097 \116\097\098\108\101\033")end return w end end return nil end function y.setState(l,S)if h.typeChecks then assert(w.of(l)==w.StatefulComponentInstance,"\073\110\118\097\108\105\100 \096\115\101\108\102\096 \097\114\103\117\109\101\110\116 \116\111 \096\101\120\116\101\110\100\096\046")end local o=l[V]local Z=o.lifecyclePhase if Z==R.ShouldUpdate or Z==R.WillUpdate or Z==R.Render then local Q=u[o.lifecyclePhase]local R=Q:format(tostring(o.componentClass))error(R,2)elseif Z==R.WillUnmount then return end local s=o.pendingState local B if typeof(S)=="\102\117\110\099\116\105\111\110"then B=S(s or l.state,l.props)if B==nil then return end elseif typeof(S)=="\116\097\098\108\101"then B=S else error("\073\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \116\111 \115\101\116\083\116\097\116\101\044 \101\120\112\101\099\116\101\100 \102\117\110\099\116\105\111\110 \111\114 \116\097\098\108\101",2)end local y if s~=nil then y=Q(s,B)else y=Q({},l.state,B)end if Z==R.Init then local R=l:__getDerivedState(l.props,y)l.state=Q(y,R)elseif Z==R.DidMount or Z==R.DidUpdate or Z==R.ReconcileChildren then local R=l:__getDerivedState(l.props,y)o.pendingState=Q(y,R)elseif Z==R.Idle then l:__update(nil,y)else local Q=u.default local R=Q:format(tostring(o.componentClass))error(R,2)end end function y.getElementTraceback(Q)return Q[V].virtualNode.currentElement.source end function y.render(Q)local R=Q[V]local w=Z:format(tostring(R.componentClass))error(w,0)end function y.__getContext(R,Q)if h.internalTypeChecks then l(w.of(R)==w.StatefulComponentInstance,"\073\110\118\097\108\105\100 \117\115\101 \111\102 \096\095\095\103\101\116\067\111\110\116\101\120\116\096")l(Q~=nil,"\067\111\110\116\101\120\116 \107\101\121 \099\097\110\110\111\116 \098\101 \110\105\108")end local S=R[V].virtualNode local u=S.context return u[Q]end function y.__addContext(u,R,S)if h.internalTypeChecks then l(w.of(u)==w.StatefulComponentInstance,"\073\110\118\097\108\105\100 \117\115\101 \111\102 \096\095\095\097\100\100\067\111\110\116\101\120\116\096")end local o=u[V].virtualNode if o.originalContext==nil then o.originalContext=o.context end local Z=o.context o.context=Q({},Z,{[R]=S})end function y.__validateProps(R,Q)if not h.propValidation then return end local w=R[V].componentClass.validateProps if w==nil then return end if typeof(w)~="\102\117\110\099\116\105\111\110"then error(("\118\097\108\105\100\097\116\101\080\114\111\112\115 \109\117\115\116 \098\101 \097 \102\117\110\099\116\105\111\110\044 \098\117\116 \105\116 \105\115 \097 \037\115\046\n\067\104\101\099\107 \116\104\101 \100\101\102\105\110\105\116\105\111\110 \111\102 \116\104\101 \099\111\109\112\111\110\101\110\116 \037\113\046"):format(typeof(w),R.__componentName))end local S,u=w(Q)if not S then u=u or"\060\086\097\108\105\100\097\116\111\114 \102\117\110\099\116\105\111\110 \100\105\100 \110\111\116 \115\117\112\112\108\121 \097 \109\101\115\115\097\103\101\062"error(("\080\114\111\112\101\114\116\121 \118\097\108\105\100\097\116\105\111\110 \102\097\105\108\101\100 \105\110 \037\115\058 \037\115\n\n\037\115"):format(R.__componentName,tostring(u),R:getElementTraceback()or"\060\101\110\097\098\108\101 \101\108\101\109\101\110\116 \116\114\097\099\101\098\097\099\107\115\062"),0)end end function y.__mount(o,S,u)if h.internalTypeChecks then l(w.of(o)==w.StatefulComponentClass,"\073\110\118\097\108\105\100 \117\115\101 \111\102 \096\095\095\109\111\117\110\116\096")l(w.of(u)==w.VirtualNode,"\069\120\112\101\099\116\101\100 \097\114\103 \035\050 \116\111 \098\101 \111\102 \116\121\112\101 \086\105\114\116\117\097\108\078\111\100\101")end local Z=u.currentElement local s=u.hostParent local B={reconciler=S;virtualNode=u;componentClass=o;lifecyclePhase=R.Init,pendingState=nil}local y={[w]=w.StatefulComponentInstance;[V]=B}setmetatable(y,o)u.instance=y local W=Z.props if o.defaultProps~=nil then W=Q({},o.defaultProps,W)end y:__validateProps(W)y.props=W local L=Q({},u.legacyContext)y._context=L y.state=Q({},y:__getDerivedState(y.props,{}))if y.init~=nil then y:init(y.props)Q(y.state,y:__getDerivedState(y.props,y.state))end u.legacyContext=y._context B.lifecyclePhase=R.Render local j=y:render()B.lifecyclePhase=R.ReconcileChildren S.updateVirtualNodeWithRenderResult(u,s,j)if y.didMount~=nil then B.lifecyclePhase=R.DidMount y:didMount()end if B.pendingState~=nil then y:__update(nil,nil)end B.lifecyclePhase=R.Idle end function y.__unmount(Q)if h.internalTypeChecks then l(w.of(Q)==w.StatefulComponentInstance,"\073\110\118\097\108\105\100 \117\115\101 \111\102 \096\095\095\117\110\109\111\117\110\116\096")end local S=Q[V]local u=S.virtualNode local o=S.reconciler if Q.willUnmount~=nil then S.lifecyclePhase=R.WillUnmount Q:willUnmount()end for Q,R in pairs(u.children)do o.unmountVirtualNode(R)end end function y.__update(u,R,S)if h.internalTypeChecks then l(w.of(u)==w.StatefulComponentInstance,"\073\110\118\097\108\105\100 \117\115\101 \111\102 \096\095\095\117\112\100\097\116\101\096")l(w.of(R)==w.Element or R==nil,"\069\120\112\101\099\116\101\100 \097\114\103 \035\049 \116\111 \098\101 \111\102 \116\121\112\101 \069\108\101\109\101\110\116 \111\114 \110\105\108")l(typeof(S)=="\116\097\098\108\101"or S==nil,"\069\120\112\101\099\116\101\100 \097\114\103 \035\050 \116\111 \098\101 \111\102 \116\121\112\101 \116\097\098\108\101 \111\114 \110\105\108")end local Z=u[V]local B=Z.componentClass local y=u.props if R~=nil then y=R.props if B.defaultProps~=nil then y=Q({},B.defaultProps,y)end u:__validateProps(y)end local W=0 repeat local R local w=nil if Z.pendingState~=nil then w=Z.pendingState Z.pendingState=nil end if S~=nil or y~=u.props then if w==nil then R=S or u.state else R=Q(w,S)end local l=u:__getDerivedState(y,R)if l~=nil then R=Q({},R,l)end S=nil else R=w end if not u:__resolveUpdate(y,R)then return false end W=W+1 if W>o then error(s:format(tostring(Z.componentClass)),3)end until Z.pendingState==nil return true end function y.__resolveUpdate(u,Q,S)if h.internalTypeChecks then l(w.of(u)==w.StatefulComponentInstance,"\073\110\118\097\108\105\100 \117\115\101 \111\102 \096\095\095\114\101\115\111\108\118\101\085\112\100\097\116\101\096")end local o=u[V]local Z=o.virtualNode local s=o.reconciler local B=u.props local y=u.state if Q==nil then Q=B end if S==nil then S=y end if u.shouldUpdate~=nil then o.lifecyclePhase=R.ShouldUpdate local w=u:shouldUpdate(Q,S)if not w then o.lifecyclePhase=R.Idle return false end end if u.willUpdate~=nil then o.lifecyclePhase=R.WillUpdate u:willUpdate(Q,S)end o.lifecyclePhase=R.Render u.props=Q u.state=S local W=Z.instance:render()o.lifecyclePhase=R.ReconcileChildren s.updateVirtualNodeWithRenderResult(Z,Z.hostParent,W)if u.didUpdate~=nil then o.lifecyclePhase=R.DidUpdate u:didUpdate(B,y)end o.lifecyclePhase=R.Idle return true end return y
local assign = require(script.Parent.assign)
local ComponentLifecyclePhase = require(script.Parent.ComponentLifecyclePhase)
local Type = require(script.Parent.Type)
local Symbol = require(script.Parent.Symbol)
local invalidSetStateMessages = require(script.Parent.invalidSetStateMessages)
local internalAssert = require(script.Parent.internalAssert)

local config = require(script.Parent.GlobalConfig).get()

--[[
	Calling setState during certain lifecycle allowed methods has the potential
	to create an infinitely updating component. Rather than time out, we exit
	with an error if an unreasonable number of self-triggering updates occur
]]
local MAX_PENDING_UPDATES = 100

local InternalData = Symbol.named("InternalData")

local componentMissingRenderMessage = [[
The component %q is missing the `render` method.
`render` must be defined when creating a Roact component!]]

local tooManyUpdatesMessage = [[
The component %q has reached the setState update recursion limit.
When using `setState` in `didUpdate`, make sure that it won't repeat infinitely!]]

local componentClassMetatable = {}

function componentClassMetatable:__tostring()
	return self.__componentName
end

local Component = {}
setmetatable(Component, componentClassMetatable)

Component[Type] = Type.StatefulComponentClass
Component.__index = Component
Component.__componentName = "Component"

--[[
	A method called by consumers of Roact to create a new component class.
	Components can not be extended beyond this point, with the exception of
	PureComponent.
]]
function Component:extend(name)
	if config.typeChecks then
		assert(Type.of(self) == Type.StatefulComponentClass, "Invalid `self` argument to `extend`.")
		assert(typeof(name) == "string", "Component class name must be a string")
	end

	local class = {}

	for key, value in pairs(self) do
		-- Roact opts to make consumers use composition over inheritance, which
		-- lines up with React.
		-- https://reactjs.org/docs/composition-vs-inheritance.html
		if key ~= "extend" then
			class[key] = value
		end
	end

	class[Type] = Type.StatefulComponentClass
	class.__index = class
	class.__componentName = name

	setmetatable(class, componentClassMetatable)

	return class
end

function Component:__getDerivedState(incomingProps, incomingState)
	if config.internalTypeChecks then
		internalAssert(Type.of(self) == Type.StatefulComponentInstance, "Invalid use of `__getDerivedState`")
	end

	local internalData = self[InternalData]
	local componentClass = internalData.componentClass

	if componentClass.getDerivedStateFromProps ~= nil then
		local derivedState = componentClass.getDerivedStateFromProps(incomingProps, incomingState)

		if derivedState ~= nil then
			if config.typeChecks then
				assert(typeof(derivedState) == "table", "getDerivedStateFromProps must return a table!")
			end

			return derivedState
		end
	end

	return nil
end

function Component:setState(mapState)
	if config.typeChecks then
		assert(Type.of(self) == Type.StatefulComponentInstance, "Invalid `self` argument to `extend`.")
	end

	local internalData = self[InternalData]
	local lifecyclePhase = internalData.lifecyclePhase

	--[[
		When preparing to update, render, or unmount, it is not safe
		to call `setState` as it will interfere with in-flight updates. It's
		also disallowed during unmounting
	]]
	if
		lifecyclePhase == ComponentLifecyclePhase.ShouldUpdate
		or lifecyclePhase == ComponentLifecyclePhase.WillUpdate
		or lifecyclePhase == ComponentLifecyclePhase.Render
	then
		local messageTemplate = invalidSetStateMessages[internalData.lifecyclePhase]

		local message = messageTemplate:format(tostring(internalData.componentClass))
		error(message, 2)
	elseif lifecyclePhase == ComponentLifecyclePhase.WillUnmount then
		-- Should not print error message. See https://github.com/facebook/react/pull/22114
		return
	end

	local pendingState = internalData.pendingState

	local partialState
	if typeof(mapState) == "function" then
		partialState = mapState(pendingState or self.state, self.props)

		-- Abort the state update if the given state updater function returns nil
		if partialState == nil then
			return
		end
	elseif typeof(mapState) == "table" then
		partialState = mapState
	else
		error("Invalid argument to setState, expected function or table", 2)
	end

	local newState
	if pendingState ~= nil then
		newState = assign(pendingState, partialState)
	else
		newState = assign({}, self.state, partialState)
	end

	if lifecyclePhase == ComponentLifecyclePhase.Init then
		-- If `setState` is called in `init`, we can skip triggering an update!
		local derivedState = self:__getDerivedState(self.props, newState)
		self.state = assign(newState, derivedState)
	elseif
		lifecyclePhase == ComponentLifecyclePhase.DidMount
		or lifecyclePhase == ComponentLifecyclePhase.DidUpdate
		or lifecyclePhase == ComponentLifecyclePhase.ReconcileChildren
	then
		--[[
			During certain phases of the component lifecycle, it's acceptable to
			allow `setState` but defer the update until we're done with ones in flight.
			We do this by collapsing it into any pending updates we have.
		]]
		local derivedState = self:__getDerivedState(self.props, newState)
		internalData.pendingState = assign(newState, derivedState)
	elseif lifecyclePhase == ComponentLifecyclePhase.Idle then
		-- Outside of our lifecycle, the state update is safe to make immediately
		self:__update(nil, newState)
	else
		local messageTemplate = invalidSetStateMessages.default

		local message = messageTemplate:format(tostring(internalData.componentClass))

		error(message, 2)
	end
end

--[[
	Returns the stack trace of where the element was created that this component
	instance's properties are based on.

	Intended to be used primarily by diagnostic tools.
]]
function Component:getElementTraceback()
	return self[InternalData].virtualNode.currentElement.source
end

--[[
	Returns a snapshot of this component given the current props and state. Must
	be overridden by consumers of Roact and should be a pure function with
	regards to props and state.

	TODO (#199): Accept props and state as arguments.
]]
function Component:render()
	local internalData = self[InternalData]

	local message = componentMissingRenderMessage:format(tostring(internalData.componentClass))

	error(message, 0)
end

--[[
	Retrieves the context value corresponding to the given key. Can return nil
	if a requested context key is not present
]]
function Component:__getContext(key)
	if config.internalTypeChecks then
		internalAssert(Type.of(self) == Type.StatefulComponentInstance, "Invalid use of `__getContext`")
		internalAssert(key ~= nil, "Context key cannot be nil")
	end

	local virtualNode = self[InternalData].virtualNode
	local context = virtualNode.context

	return context[key]
end

--[[
	Adds a new context entry to this component's context table (which will be
	passed down to child components).
]]
function Component:__addContext(key, value)
	if config.internalTypeChecks then
		internalAssert(Type.of(self) == Type.StatefulComponentInstance, "Invalid use of `__addContext`")
	end
	local virtualNode = self[InternalData].virtualNode

	-- Make sure we store a reference to the component's original, unmodified
	-- context the virtual node. In the reconciler, we'll restore the original
	-- context if we need to replace the node (this happens when a node gets
	-- re-rendered as a different component)
	if virtualNode.originalContext == nil then
		virtualNode.originalContext = virtualNode.context
	end

	-- Build a new context table on top of the existing one, then apply it to
	-- our virtualNode
	local existing = virtualNode.context
	virtualNode.context = assign({}, existing, { [key] = value })
end

--[[
	Performs property validation if the static method validateProps is declared.
	validateProps should follow assert's expected arguments:
	(false, message: string) | true. The function may return a message in the
	true case; it will be ignored. If this fails, the function will throw the
	error.
]]
function Component:__validateProps(props)
	if not config.propValidation then
		return
	end

	local validator = self[InternalData].componentClass.validateProps

	if validator == nil then
		return
	end

	if typeof(validator) ~= "function" then
		error(
			("validateProps must be a function, but it is a %s.\nCheck the definition of the component %q."):format(
				typeof(validator),
				self.__componentName
			)
		)
	end

	local success, failureReason = validator(props)

	if not success then
		failureReason = failureReason or "<Validator function did not supply a message>"
		error(
			("Property validation failed in %s: %s\n\n%s"):format(
				self.__componentName,
				tostring(failureReason),
				self:getElementTraceback() or "<enable element tracebacks>"
			),
			0
		)
	end
end

--[[
	An internal method used by the reconciler to construct a new component
	instance and attach it to the given virtualNode.
]]
function Component:__mount(reconciler, virtualNode)
	if config.internalTypeChecks then
		internalAssert(Type.of(self) == Type.StatefulComponentClass, "Invalid use of `__mount`")
		internalAssert(Type.of(virtualNode) == Type.VirtualNode, "Expected arg #2 to be of type VirtualNode")
	end

	local currentElement = virtualNode.currentElement
	local hostParent = virtualNode.hostParent

	-- Contains all the information that we want to keep from consumers of
	-- Roact, or even other parts of the codebase like the reconciler.
	local internalData = {
		reconciler = reconciler,
		virtualNode = virtualNode,
		componentClass = self,
		lifecyclePhase = ComponentLifecyclePhase.Init,
		pendingState = nil,
	}

	local instance = {
		[Type] = Type.StatefulComponentInstance,
		[InternalData] = internalData,
	}

	setmetatable(instance, self)

	virtualNode.instance = instance

	local props = currentElement.props

	if self.defaultProps ~= nil then
		props = assign({}, self.defaultProps, props)
	end

	instance:__validateProps(props)

	instance.props = props

	local newContext = assign({}, virtualNode.legacyContext)
	instance._context = newContext

	instance.state = assign({}, instance:__getDerivedState(instance.props, {}))

	if instance.init ~= nil then
		instance:init(instance.props)
		assign(instance.state, instance:__getDerivedState(instance.props, instance.state))
	end

	-- It's possible for init() to redefine _context!
	virtualNode.legacyContext = instance._context

	internalData.lifecyclePhase = ComponentLifecyclePhase.Render
	local renderResult = instance:render()

	internalData.lifecyclePhase = ComponentLifecyclePhase.ReconcileChildren
	reconciler.updateVirtualNodeWithRenderResult(virtualNode, hostParent, renderResult)

	if instance.didMount ~= nil then
		internalData.lifecyclePhase = ComponentLifecyclePhase.DidMount
		instance:didMount()
	end

	if internalData.pendingState ~= nil then
		-- __update will handle pendingState, so we don't pass any new element or state
		instance:__update(nil, nil)
	end

	internalData.lifecyclePhase = ComponentLifecyclePhase.Idle
end

--[[
	Internal method used by the reconciler to clean up any resources held by
	this component instance.
]]
function Component:__unmount()
	if config.internalTypeChecks then
		internalAssert(Type.of(self) == Type.StatefulComponentInstance, "Invalid use of `__unmount`")
	end

	local internalData = self[InternalData]
	local virtualNode = internalData.virtualNode
	local reconciler = internalData.reconciler

	if self.willUnmount ~= nil then
		internalData.lifecyclePhase = ComponentLifecyclePhase.WillUnmount
		self:willUnmount()
	end

	for _, childNode in pairs(virtualNode.children) do
		reconciler.unmountVirtualNode(childNode)
	end
end

--[[
	Internal method used by setState (to trigger updates based on state) and by
	the reconciler (to trigger updates based on props)

	Returns true if the update was completed, false if it was cancelled by shouldUpdate
]]
function Component:__update(updatedElement, updatedState)
	if config.internalTypeChecks then
		internalAssert(Type.of(self) == Type.StatefulComponentInstance, "Invalid use of `__update`")
		internalAssert(
			Type.of(updatedElement) == Type.Element or updatedElement == nil,
			"Expected arg #1 to be of type Element or nil"
		)
		internalAssert(
			typeof(updatedState) == "table" or updatedState == nil,
			"Expected arg #2 to be of type table or nil"
		)
	end

	local internalData = self[InternalData]
	local componentClass = internalData.componentClass

	local newProps = self.props
	if updatedElement ~= nil then
		newProps = updatedElement.props

		if componentClass.defaultProps ~= nil then
			newProps = assign({}, componentClass.defaultProps, newProps)
		end

		self:__validateProps(newProps)
	end

	local updateCount = 0
	repeat
		local finalState
		local pendingState = nil

		-- Consume any pending state we might have
		if internalData.pendingState ~= nil then
			pendingState = internalData.pendingState
			internalData.pendingState = nil
		end

		-- Consume a standard update to state or props
		if updatedState ~= nil or newProps ~= self.props then
			if pendingState == nil then
				finalState = updatedState or self.state
			else
				finalState = assign(pendingState, updatedState)
			end

			local derivedState = self:__getDerivedState(newProps, finalState)

			if derivedState ~= nil then
				finalState = assign({}, finalState, derivedState)
			end

			updatedState = nil
		else
			finalState = pendingState
		end

		if not self:__resolveUpdate(newProps, finalState) then
			-- If the update was short-circuited, bubble the result up to the caller
			return false
		end

		updateCount = updateCount + 1

		if updateCount > MAX_PENDING_UPDATES then
			error(tooManyUpdatesMessage:format(tostring(internalData.componentClass)), 3)
		end
	until internalData.pendingState == nil

	return true
end

--[[
	Internal method used by __update to apply new props and state

	Returns true if the update was completed, false if it was cancelled by shouldUpdate
]]
function Component:__resolveUpdate(incomingProps, incomingState)
	if config.internalTypeChecks then
		internalAssert(Type.of(self) == Type.StatefulComponentInstance, "Invalid use of `__resolveUpdate`")
	end

	local internalData = self[InternalData]
	local virtualNode = internalData.virtualNode
	local reconciler = internalData.reconciler

	local oldProps = self.props
	local oldState = self.state

	if incomingProps == nil then
		incomingProps = oldProps
	end
	if incomingState == nil then
		incomingState = oldState
	end

	if self.shouldUpdate ~= nil then
		internalData.lifecyclePhase = ComponentLifecyclePhase.ShouldUpdate
		local continueWithUpdate = self:shouldUpdate(incomingProps, incomingState)

		if not continueWithUpdate then
			internalData.lifecyclePhase = ComponentLifecyclePhase.Idle
			return false
		end
	end

	if self.willUpdate ~= nil then
		internalData.lifecyclePhase = ComponentLifecyclePhase.WillUpdate
		self:willUpdate(incomingProps, incomingState)
	end

	internalData.lifecyclePhase = ComponentLifecyclePhase.Render

	self.props = incomingProps
	self.state = incomingState

	local renderResult = virtualNode.instance:render()

	internalData.lifecyclePhase = ComponentLifecyclePhase.ReconcileChildren
	reconciler.updateVirtualNodeWithRenderResult(virtualNode, virtualNode.hostParent, renderResult)

	if self.didUpdate ~= nil then
		internalData.lifecyclePhase = ComponentLifecyclePhase.DidUpdate
		self:didUpdate(oldProps, oldState)
	end

	internalData.lifecyclePhase = ComponentLifecyclePhase.Idle
	return true
end

return Component
