React stale error date on child componentsHow do I format a Microsoft JSON date?Compare two dates with JavaScriptDetecting an “invalid date” Date instance in JavaScriptHow do I get the current date in JavaScript?How to format a JavaScript dateWhy does my JavaScript get a “No 'Access-Control-Allow-Origin' header is present on the requested resource” error when Postman does not?Loop inside React JSXWhat do these three dots in React do?Programmatically navigate using react routerHow get value datapicker in react toobox custom?
Showing mass murder in a kid's book
What is the meaning of "You've never met a graph you didn't like?"
Can I say "fingers" when referring to toes?
Do you waste sorcery points if you try to apply metamagic to a spell from a scroll but fail to cast it?
Should I warn new/interviewing PhD Student that supervisor is terrible?
Limit max CPU usage SQL SERVER with WSRM
Is there a RAID 0 Equivalent for RAM?
"Oh no!" in Latin
Air travel with refrigerated insulin
Anime with legendary swords made from talismans and a man who could change them with a shattered body
If the only attacker is removed from combat, is a creature still counted as having attacked this turn?
Did I make a mistake by ccing email to boss to others?
Would a primitive species be able to learn English from reading books alone?
When and why was runway 07/25 at Kai Tak removed?
How to make a list of partial sums using forEach
Can you identify this lizard-like creature I observed in the UK?
Sigmoid with a slope but no asymptotes?
How were servants to the Kaiser of Imperial Germany treated and where may I find more information on them
How do I Interface a PS/2 Keyboard without Modern Techniques?
Proving an identity involving cross products and coplanar vectors
Should I assume I have passed probation?
Does the Crossbow Expert feat's extra crossbow attack work with the reaction attack from a Hunter ranger's Giant Killer feature?
Why is the principal energy of an electron lower for excited electrons in a higher energy state?
Does Doodling or Improvising on the Piano Have Any Benefits?
React stale error date on child components
How do I format a Microsoft JSON date?Compare two dates with JavaScriptDetecting an “invalid date” Date instance in JavaScriptHow do I get the current date in JavaScript?How to format a JavaScript dateWhy does my JavaScript get a “No 'Access-Control-Allow-Origin' header is present on the requested resource” error when Postman does not?Loop inside React JSXWhat do these three dots in React do?Programmatically navigate using react routerHow get value datapicker in react toobox custom?
I'm trying to have form input elements, which are uncontrolled because of our use of jQuery UI DatePicker and jQuery maskMoney, render errors underneath them as soon as user types something invalid for that field, as well as disable the button on any of the errors. For some reason, none of that is working right.
Main component
is something like the following:
class MainComponent extends React.Component
constructor(props)
super(props)
this.state =
payrates: [
new PayRate(new Date(2019, 2, 1), 0.00),
],
errors :
rate: '',
date: ''
,
currentPayRate : new PayRate() // has Rate and EffectiveDate fields
// binding done here
this.appendValue = this.appendValue.bind(this)
this.updateCurrentPayRate = this.updateCurrentPayRate.bind(this)
this.updateCurrentPayRateDate = this.updateCurrentPayRateDate.bind(this)
this.updateCurrentPayRateAmount = this.updateCurrentPayRateAmount.bind(this)
this.validate = this.validate.bind(this)
/**
* @param PayRate newPayRate
**/
updateCurrentPayRate(newPayRate)
this.setState(
...this.state,
currentPayRate : newPayRate
)
updateCurrentPayRateDate(dateString)
const newPayRate = Object.assign(new PayRate(), this.state.currentPayRate, EffectiveDate : new Date(dateString) )
this.validate(newPayRate)
this.updateCurrentPayRate(newPayRate)
updateCurrentPayRateAmount(amount)
const newPayRate = Object.assign(new PayRate(), this.state.currentPayRate, Rate : Number(amount) )
this.validate(newPayRate)
this.updateCurrentPayRate(newPayRate)
/**
* @param PayRate value
**/
appendValue(value)
console.log("trying to append value: ", value)
if (this.validate(value))
this.setState(...this.state,
payrates : this.state.payrates.concat(this.state.currentPayRate))
/**
* @param PayRate value
**/
validate(value)
// extract rate,date from value
const rate = value.Rate,
date = value.EffectiveDate
console.log("value == ", value)
let errors =
// rate better resolve to something
if (!rate)
errors.rate = "Enter a valid pay rate amount"
// date better be valid
if ((!date)
render()
return <div>
<DateList dates=this.state.payrates/>
<NewPayRateRow
value=this.state.currentPayRate
errors=this.state.errors
onChange=this.updateCurrentPayRate
onPayRateAmountChange=this.updateCurrentPayRateAmount
onPayRateDateChange=this.updateCurrentPayRateDate
onAdd=this.appendValue
/>
</div>
The "form" component
Has the following implementation:
class NewPayRateRow extends React.Component
constructor(props)
super(props)
render()
console.log(Object.values(this.props.errors).filter((error) => error))
return <span class="form-inline">
<RateField
errors=this.props.errors.rate
onKeyUp=(e) =>
// extract the value
const value = e.target.value
this.props.onPayRateAmountChange(value)
/>
<DateInput
errors=this.props.errors.date
onChange=this.props.onPayRateDateChange
/>
<button onClick=(e) =>
this.props.onAdd(this.props.value)
disabled=Object.values(this.props.errors).filter((error) => error).length>Add New Pay Rate</button>
</span>
An uncontrolled input component
where the issue definitely happens:
class DateInput extends React.Component
constructor(props)
super(props);
// do bindings
this.handleChange = this.handleChange.bind(this);
componentDidMount()
$('#datepicker').datepicker(
changeMonth: true,
changeYear: true,
showButtonPanel: true,
yearRange: "-116:+34",
dateFormat: 'mm/dd/yy',
// telling jQuery UI to pass its event to React
onSelect : this.handleChange
);
componentWillUnmount()
$('#datepicker').datepicker('destroy')
// handles a change to the input field
handleChange(value)
this.props.onChange(value)
render() ''
return <div class="col-md-2">
<input
id="datepicker"
className="datepicker form-control " + fieldIsInvalid
placeholder="mm/dd/yyyy"
onChange=(e) => this.props.onChange(e.target.value) >
</input>
<div>
this.props.errors
</div>
</div>
For some reason, even though I'm selecting via the datepicker widget the value, the errors
don't change:
However, when I go to comment out all the validate
calls, it adds the fields no problem.
I did some caveman debugging on the value
I was passing to validate
to ensure that I was passing it truthy data.
Why is this.state.error
not updating correctly, via the components?!
UPDATE: I went to update just the pay rate, initially, and the errors rendered correctly, and from going through the code, I found that this.setState
was actually setting the state. However, when I went to trigger change on the input money field, this.setState
was getting hit, and errors
object, was empty (which is correct), but somehow, this.setState
wasn't actually updating the state.
javascript jquery reactjs
add a comment |
I'm trying to have form input elements, which are uncontrolled because of our use of jQuery UI DatePicker and jQuery maskMoney, render errors underneath them as soon as user types something invalid for that field, as well as disable the button on any of the errors. For some reason, none of that is working right.
Main component
is something like the following:
class MainComponent extends React.Component
constructor(props)
super(props)
this.state =
payrates: [
new PayRate(new Date(2019, 2, 1), 0.00),
],
errors :
rate: '',
date: ''
,
currentPayRate : new PayRate() // has Rate and EffectiveDate fields
// binding done here
this.appendValue = this.appendValue.bind(this)
this.updateCurrentPayRate = this.updateCurrentPayRate.bind(this)
this.updateCurrentPayRateDate = this.updateCurrentPayRateDate.bind(this)
this.updateCurrentPayRateAmount = this.updateCurrentPayRateAmount.bind(this)
this.validate = this.validate.bind(this)
/**
* @param PayRate newPayRate
**/
updateCurrentPayRate(newPayRate)
this.setState(
...this.state,
currentPayRate : newPayRate
)
updateCurrentPayRateDate(dateString)
const newPayRate = Object.assign(new PayRate(), this.state.currentPayRate, EffectiveDate : new Date(dateString) )
this.validate(newPayRate)
this.updateCurrentPayRate(newPayRate)
updateCurrentPayRateAmount(amount)
const newPayRate = Object.assign(new PayRate(), this.state.currentPayRate, Rate : Number(amount) )
this.validate(newPayRate)
this.updateCurrentPayRate(newPayRate)
/**
* @param PayRate value
**/
appendValue(value)
console.log("trying to append value: ", value)
if (this.validate(value))
this.setState(...this.state,
payrates : this.state.payrates.concat(this.state.currentPayRate))
/**
* @param PayRate value
**/
validate(value)
// extract rate,date from value
const rate = value.Rate,
date = value.EffectiveDate
console.log("value == ", value)
let errors =
// rate better resolve to something
if (!rate)
errors.rate = "Enter a valid pay rate amount"
// date better be valid
if ((!date)
render()
return <div>
<DateList dates=this.state.payrates/>
<NewPayRateRow
value=this.state.currentPayRate
errors=this.state.errors
onChange=this.updateCurrentPayRate
onPayRateAmountChange=this.updateCurrentPayRateAmount
onPayRateDateChange=this.updateCurrentPayRateDate
onAdd=this.appendValue
/>
</div>
The "form" component
Has the following implementation:
class NewPayRateRow extends React.Component
constructor(props)
super(props)
render()
console.log(Object.values(this.props.errors).filter((error) => error))
return <span class="form-inline">
<RateField
errors=this.props.errors.rate
onKeyUp=(e) =>
// extract the value
const value = e.target.value
this.props.onPayRateAmountChange(value)
/>
<DateInput
errors=this.props.errors.date
onChange=this.props.onPayRateDateChange
/>
<button onClick=(e) =>
this.props.onAdd(this.props.value)
disabled=Object.values(this.props.errors).filter((error) => error).length>Add New Pay Rate</button>
</span>
An uncontrolled input component
where the issue definitely happens:
class DateInput extends React.Component
constructor(props)
super(props);
// do bindings
this.handleChange = this.handleChange.bind(this);
componentDidMount()
$('#datepicker').datepicker(
changeMonth: true,
changeYear: true,
showButtonPanel: true,
yearRange: "-116:+34",
dateFormat: 'mm/dd/yy',
// telling jQuery UI to pass its event to React
onSelect : this.handleChange
);
componentWillUnmount()
$('#datepicker').datepicker('destroy')
// handles a change to the input field
handleChange(value)
this.props.onChange(value)
render() ''
return <div class="col-md-2">
<input
id="datepicker"
className="datepicker form-control " + fieldIsInvalid
placeholder="mm/dd/yyyy"
onChange=(e) => this.props.onChange(e.target.value) >
</input>
<div>
this.props.errors
</div>
</div>
For some reason, even though I'm selecting via the datepicker widget the value, the errors
don't change:
However, when I go to comment out all the validate
calls, it adds the fields no problem.
I did some caveman debugging on the value
I was passing to validate
to ensure that I was passing it truthy data.
Why is this.state.error
not updating correctly, via the components?!
UPDATE: I went to update just the pay rate, initially, and the errors rendered correctly, and from going through the code, I found that this.setState
was actually setting the state. However, when I went to trigger change on the input money field, this.setState
was getting hit, and errors
object, was empty (which is correct), but somehow, this.setState
wasn't actually updating the state.
javascript jquery reactjs
add a comment |
I'm trying to have form input elements, which are uncontrolled because of our use of jQuery UI DatePicker and jQuery maskMoney, render errors underneath them as soon as user types something invalid for that field, as well as disable the button on any of the errors. For some reason, none of that is working right.
Main component
is something like the following:
class MainComponent extends React.Component
constructor(props)
super(props)
this.state =
payrates: [
new PayRate(new Date(2019, 2, 1), 0.00),
],
errors :
rate: '',
date: ''
,
currentPayRate : new PayRate() // has Rate and EffectiveDate fields
// binding done here
this.appendValue = this.appendValue.bind(this)
this.updateCurrentPayRate = this.updateCurrentPayRate.bind(this)
this.updateCurrentPayRateDate = this.updateCurrentPayRateDate.bind(this)
this.updateCurrentPayRateAmount = this.updateCurrentPayRateAmount.bind(this)
this.validate = this.validate.bind(this)
/**
* @param PayRate newPayRate
**/
updateCurrentPayRate(newPayRate)
this.setState(
...this.state,
currentPayRate : newPayRate
)
updateCurrentPayRateDate(dateString)
const newPayRate = Object.assign(new PayRate(), this.state.currentPayRate, EffectiveDate : new Date(dateString) )
this.validate(newPayRate)
this.updateCurrentPayRate(newPayRate)
updateCurrentPayRateAmount(amount)
const newPayRate = Object.assign(new PayRate(), this.state.currentPayRate, Rate : Number(amount) )
this.validate(newPayRate)
this.updateCurrentPayRate(newPayRate)
/**
* @param PayRate value
**/
appendValue(value)
console.log("trying to append value: ", value)
if (this.validate(value))
this.setState(...this.state,
payrates : this.state.payrates.concat(this.state.currentPayRate))
/**
* @param PayRate value
**/
validate(value)
// extract rate,date from value
const rate = value.Rate,
date = value.EffectiveDate
console.log("value == ", value)
let errors =
// rate better resolve to something
if (!rate)
errors.rate = "Enter a valid pay rate amount"
// date better be valid
if ((!date)
render()
return <div>
<DateList dates=this.state.payrates/>
<NewPayRateRow
value=this.state.currentPayRate
errors=this.state.errors
onChange=this.updateCurrentPayRate
onPayRateAmountChange=this.updateCurrentPayRateAmount
onPayRateDateChange=this.updateCurrentPayRateDate
onAdd=this.appendValue
/>
</div>
The "form" component
Has the following implementation:
class NewPayRateRow extends React.Component
constructor(props)
super(props)
render()
console.log(Object.values(this.props.errors).filter((error) => error))
return <span class="form-inline">
<RateField
errors=this.props.errors.rate
onKeyUp=(e) =>
// extract the value
const value = e.target.value
this.props.onPayRateAmountChange(value)
/>
<DateInput
errors=this.props.errors.date
onChange=this.props.onPayRateDateChange
/>
<button onClick=(e) =>
this.props.onAdd(this.props.value)
disabled=Object.values(this.props.errors).filter((error) => error).length>Add New Pay Rate</button>
</span>
An uncontrolled input component
where the issue definitely happens:
class DateInput extends React.Component
constructor(props)
super(props);
// do bindings
this.handleChange = this.handleChange.bind(this);
componentDidMount()
$('#datepicker').datepicker(
changeMonth: true,
changeYear: true,
showButtonPanel: true,
yearRange: "-116:+34",
dateFormat: 'mm/dd/yy',
// telling jQuery UI to pass its event to React
onSelect : this.handleChange
);
componentWillUnmount()
$('#datepicker').datepicker('destroy')
// handles a change to the input field
handleChange(value)
this.props.onChange(value)
render() ''
return <div class="col-md-2">
<input
id="datepicker"
className="datepicker form-control " + fieldIsInvalid
placeholder="mm/dd/yyyy"
onChange=(e) => this.props.onChange(e.target.value) >
</input>
<div>
this.props.errors
</div>
</div>
For some reason, even though I'm selecting via the datepicker widget the value, the errors
don't change:
However, when I go to comment out all the validate
calls, it adds the fields no problem.
I did some caveman debugging on the value
I was passing to validate
to ensure that I was passing it truthy data.
Why is this.state.error
not updating correctly, via the components?!
UPDATE: I went to update just the pay rate, initially, and the errors rendered correctly, and from going through the code, I found that this.setState
was actually setting the state. However, when I went to trigger change on the input money field, this.setState
was getting hit, and errors
object, was empty (which is correct), but somehow, this.setState
wasn't actually updating the state.
javascript jquery reactjs
I'm trying to have form input elements, which are uncontrolled because of our use of jQuery UI DatePicker and jQuery maskMoney, render errors underneath them as soon as user types something invalid for that field, as well as disable the button on any of the errors. For some reason, none of that is working right.
Main component
is something like the following:
class MainComponent extends React.Component
constructor(props)
super(props)
this.state =
payrates: [
new PayRate(new Date(2019, 2, 1), 0.00),
],
errors :
rate: '',
date: ''
,
currentPayRate : new PayRate() // has Rate and EffectiveDate fields
// binding done here
this.appendValue = this.appendValue.bind(this)
this.updateCurrentPayRate = this.updateCurrentPayRate.bind(this)
this.updateCurrentPayRateDate = this.updateCurrentPayRateDate.bind(this)
this.updateCurrentPayRateAmount = this.updateCurrentPayRateAmount.bind(this)
this.validate = this.validate.bind(this)
/**
* @param PayRate newPayRate
**/
updateCurrentPayRate(newPayRate)
this.setState(
...this.state,
currentPayRate : newPayRate
)
updateCurrentPayRateDate(dateString)
const newPayRate = Object.assign(new PayRate(), this.state.currentPayRate, EffectiveDate : new Date(dateString) )
this.validate(newPayRate)
this.updateCurrentPayRate(newPayRate)
updateCurrentPayRateAmount(amount)
const newPayRate = Object.assign(new PayRate(), this.state.currentPayRate, Rate : Number(amount) )
this.validate(newPayRate)
this.updateCurrentPayRate(newPayRate)
/**
* @param PayRate value
**/
appendValue(value)
console.log("trying to append value: ", value)
if (this.validate(value))
this.setState(...this.state,
payrates : this.state.payrates.concat(this.state.currentPayRate))
/**
* @param PayRate value
**/
validate(value)
// extract rate,date from value
const rate = value.Rate,
date = value.EffectiveDate
console.log("value == ", value)
let errors =
// rate better resolve to something
if (!rate)
errors.rate = "Enter a valid pay rate amount"
// date better be valid
if ((!date)
render()
return <div>
<DateList dates=this.state.payrates/>
<NewPayRateRow
value=this.state.currentPayRate
errors=this.state.errors
onChange=this.updateCurrentPayRate
onPayRateAmountChange=this.updateCurrentPayRateAmount
onPayRateDateChange=this.updateCurrentPayRateDate
onAdd=this.appendValue
/>
</div>
The "form" component
Has the following implementation:
class NewPayRateRow extends React.Component
constructor(props)
super(props)
render()
console.log(Object.values(this.props.errors).filter((error) => error))
return <span class="form-inline">
<RateField
errors=this.props.errors.rate
onKeyUp=(e) =>
// extract the value
const value = e.target.value
this.props.onPayRateAmountChange(value)
/>
<DateInput
errors=this.props.errors.date
onChange=this.props.onPayRateDateChange
/>
<button onClick=(e) =>
this.props.onAdd(this.props.value)
disabled=Object.values(this.props.errors).filter((error) => error).length>Add New Pay Rate</button>
</span>
An uncontrolled input component
where the issue definitely happens:
class DateInput extends React.Component
constructor(props)
super(props);
// do bindings
this.handleChange = this.handleChange.bind(this);
componentDidMount()
$('#datepicker').datepicker(
changeMonth: true,
changeYear: true,
showButtonPanel: true,
yearRange: "-116:+34",
dateFormat: 'mm/dd/yy',
// telling jQuery UI to pass its event to React
onSelect : this.handleChange
);
componentWillUnmount()
$('#datepicker').datepicker('destroy')
// handles a change to the input field
handleChange(value)
this.props.onChange(value)
render() ''
return <div class="col-md-2">
<input
id="datepicker"
className="datepicker form-control " + fieldIsInvalid
placeholder="mm/dd/yyyy"
onChange=(e) => this.props.onChange(e.target.value) >
</input>
<div>
this.props.errors
</div>
</div>
For some reason, even though I'm selecting via the datepicker widget the value, the errors
don't change:
However, when I go to comment out all the validate
calls, it adds the fields no problem.
I did some caveman debugging on the value
I was passing to validate
to ensure that I was passing it truthy data.
Why is this.state.error
not updating correctly, via the components?!
UPDATE: I went to update just the pay rate, initially, and the errors rendered correctly, and from going through the code, I found that this.setState
was actually setting the state. However, when I went to trigger change on the input money field, this.setState
was getting hit, and errors
object, was empty (which is correct), but somehow, this.setState
wasn't actually updating the state.
javascript jquery reactjs
javascript jquery reactjs
edited Mar 11 at 15:01
Mike Warren
asked Mar 7 at 22:02
Mike WarrenMike Warren
1,38821643
1,38821643
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I fixed the issue!
What I did
Instead of persisting errors
in the global state
, and instead of passing validate
, to set the global state, to the methods, I maintain it as function defined outside the main component's class, like this :
/**
* Validates a PayRate
* @param PayRate value
* @returns Object any errors
**/
function validate(value = )
// extract rate,date from value
const rate = value.Rate,
date = value.EffectiveDate
let errors =
// rate better resolve to something
if (!rate)
errors.rate = "Enter a valid pay rate amount"
// date better be valid
if ((!date)
Note the much simpler implementation. I then no longer need to call validate
on the updateCurrentPayRate...
methods.
Instead, I invoke it on NewPayRateRow.render
(which I can now do because it's not touching state at all, avoiding any invariant violation), save the result to a local const variable, called errors
, and use that instead of this.props.errors
. Though, truth be told, I could probably put validate
back in this.props
to achieve a layer of abstraction/extensibility.
Also, I took Pagoaga's advice and used className
instead of class
(I don't have that as muscle memory yet).
1
Nice job, always good to see follow-ups when people solve their own problem.
– Jacob
Mar 12 at 15:45
add a comment |
You have a "class" attribute inside several of your render functions, replacing it with "className" will allow the error to show up : https://codepen.io/BPagoaga/pen/QoMXmw
return <div className="col-md-2">
The error shows up (thank you for fixing that!), but now the issue remains oferror
state being stale after initial render
– Mike Warren
Mar 11 at 15:52
I fixed the issue. Turns out that I was overly complicating it...
– Mike Warren
Mar 11 at 18:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55053503%2freact-stale-error-date-on-child-components%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I fixed the issue!
What I did
Instead of persisting errors
in the global state
, and instead of passing validate
, to set the global state, to the methods, I maintain it as function defined outside the main component's class, like this :
/**
* Validates a PayRate
* @param PayRate value
* @returns Object any errors
**/
function validate(value = )
// extract rate,date from value
const rate = value.Rate,
date = value.EffectiveDate
let errors =
// rate better resolve to something
if (!rate)
errors.rate = "Enter a valid pay rate amount"
// date better be valid
if ((!date)
Note the much simpler implementation. I then no longer need to call validate
on the updateCurrentPayRate...
methods.
Instead, I invoke it on NewPayRateRow.render
(which I can now do because it's not touching state at all, avoiding any invariant violation), save the result to a local const variable, called errors
, and use that instead of this.props.errors
. Though, truth be told, I could probably put validate
back in this.props
to achieve a layer of abstraction/extensibility.
Also, I took Pagoaga's advice and used className
instead of class
(I don't have that as muscle memory yet).
1
Nice job, always good to see follow-ups when people solve their own problem.
– Jacob
Mar 12 at 15:45
add a comment |
I fixed the issue!
What I did
Instead of persisting errors
in the global state
, and instead of passing validate
, to set the global state, to the methods, I maintain it as function defined outside the main component's class, like this :
/**
* Validates a PayRate
* @param PayRate value
* @returns Object any errors
**/
function validate(value = )
// extract rate,date from value
const rate = value.Rate,
date = value.EffectiveDate
let errors =
// rate better resolve to something
if (!rate)
errors.rate = "Enter a valid pay rate amount"
// date better be valid
if ((!date)
Note the much simpler implementation. I then no longer need to call validate
on the updateCurrentPayRate...
methods.
Instead, I invoke it on NewPayRateRow.render
(which I can now do because it's not touching state at all, avoiding any invariant violation), save the result to a local const variable, called errors
, and use that instead of this.props.errors
. Though, truth be told, I could probably put validate
back in this.props
to achieve a layer of abstraction/extensibility.
Also, I took Pagoaga's advice and used className
instead of class
(I don't have that as muscle memory yet).
1
Nice job, always good to see follow-ups when people solve their own problem.
– Jacob
Mar 12 at 15:45
add a comment |
I fixed the issue!
What I did
Instead of persisting errors
in the global state
, and instead of passing validate
, to set the global state, to the methods, I maintain it as function defined outside the main component's class, like this :
/**
* Validates a PayRate
* @param PayRate value
* @returns Object any errors
**/
function validate(value = )
// extract rate,date from value
const rate = value.Rate,
date = value.EffectiveDate
let errors =
// rate better resolve to something
if (!rate)
errors.rate = "Enter a valid pay rate amount"
// date better be valid
if ((!date)
Note the much simpler implementation. I then no longer need to call validate
on the updateCurrentPayRate...
methods.
Instead, I invoke it on NewPayRateRow.render
(which I can now do because it's not touching state at all, avoiding any invariant violation), save the result to a local const variable, called errors
, and use that instead of this.props.errors
. Though, truth be told, I could probably put validate
back in this.props
to achieve a layer of abstraction/extensibility.
Also, I took Pagoaga's advice and used className
instead of class
(I don't have that as muscle memory yet).
I fixed the issue!
What I did
Instead of persisting errors
in the global state
, and instead of passing validate
, to set the global state, to the methods, I maintain it as function defined outside the main component's class, like this :
/**
* Validates a PayRate
* @param PayRate value
* @returns Object any errors
**/
function validate(value = )
// extract rate,date from value
const rate = value.Rate,
date = value.EffectiveDate
let errors =
// rate better resolve to something
if (!rate)
errors.rate = "Enter a valid pay rate amount"
// date better be valid
if ((!date)
Note the much simpler implementation. I then no longer need to call validate
on the updateCurrentPayRate...
methods.
Instead, I invoke it on NewPayRateRow.render
(which I can now do because it's not touching state at all, avoiding any invariant violation), save the result to a local const variable, called errors
, and use that instead of this.props.errors
. Though, truth be told, I could probably put validate
back in this.props
to achieve a layer of abstraction/extensibility.
Also, I took Pagoaga's advice and used className
instead of class
(I don't have that as muscle memory yet).
answered Mar 11 at 17:48
Mike WarrenMike Warren
1,38821643
1,38821643
1
Nice job, always good to see follow-ups when people solve their own problem.
– Jacob
Mar 12 at 15:45
add a comment |
1
Nice job, always good to see follow-ups when people solve their own problem.
– Jacob
Mar 12 at 15:45
1
1
Nice job, always good to see follow-ups when people solve their own problem.
– Jacob
Mar 12 at 15:45
Nice job, always good to see follow-ups when people solve their own problem.
– Jacob
Mar 12 at 15:45
add a comment |
You have a "class" attribute inside several of your render functions, replacing it with "className" will allow the error to show up : https://codepen.io/BPagoaga/pen/QoMXmw
return <div className="col-md-2">
The error shows up (thank you for fixing that!), but now the issue remains oferror
state being stale after initial render
– Mike Warren
Mar 11 at 15:52
I fixed the issue. Turns out that I was overly complicating it...
– Mike Warren
Mar 11 at 18:31
add a comment |
You have a "class" attribute inside several of your render functions, replacing it with "className" will allow the error to show up : https://codepen.io/BPagoaga/pen/QoMXmw
return <div className="col-md-2">
The error shows up (thank you for fixing that!), but now the issue remains oferror
state being stale after initial render
– Mike Warren
Mar 11 at 15:52
I fixed the issue. Turns out that I was overly complicating it...
– Mike Warren
Mar 11 at 18:31
add a comment |
You have a "class" attribute inside several of your render functions, replacing it with "className" will allow the error to show up : https://codepen.io/BPagoaga/pen/QoMXmw
return <div className="col-md-2">
You have a "class" attribute inside several of your render functions, replacing it with "className" will allow the error to show up : https://codepen.io/BPagoaga/pen/QoMXmw
return <div className="col-md-2">
answered Mar 11 at 15:27
Bernard PagoagaBernard Pagoaga
423210
423210
The error shows up (thank you for fixing that!), but now the issue remains oferror
state being stale after initial render
– Mike Warren
Mar 11 at 15:52
I fixed the issue. Turns out that I was overly complicating it...
– Mike Warren
Mar 11 at 18:31
add a comment |
The error shows up (thank you for fixing that!), but now the issue remains oferror
state being stale after initial render
– Mike Warren
Mar 11 at 15:52
I fixed the issue. Turns out that I was overly complicating it...
– Mike Warren
Mar 11 at 18:31
The error shows up (thank you for fixing that!), but now the issue remains of
error
state being stale after initial render– Mike Warren
Mar 11 at 15:52
The error shows up (thank you for fixing that!), but now the issue remains of
error
state being stale after initial render– Mike Warren
Mar 11 at 15:52
I fixed the issue. Turns out that I was overly complicating it...
– Mike Warren
Mar 11 at 18:31
I fixed the issue. Turns out that I was overly complicating it...
– Mike Warren
Mar 11 at 18:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55053503%2freact-stale-error-date-on-child-components%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown