React:模板样式属性不是由setState更新的(React : Template Style property is not update by setState)

编程入门 行业动态 更新时间:2024-10-06 16:25:11
React:模板样式属性不是由setState更新的(React : Template Style property is not update by setState)

考虑我的代码示例(忘记密码表单)

import React, { PropTypes, Component } from 'react'; import $ from 'jquery'; import ManageCountry from '../ManageCountry'; import './style.css'; import {CONFIG} from '../const.js'; export default class Forgotpwd extends Component { state = { passwordText : "", passwordTextConfirm : "", expirated_reset_request: "none" } trad = {}; constructor () { super(); ManageCountry.applyTradToState("single_sign_on", this.trad, () => { var trad = this.trad.message.reset_password_form[0]; this.template = ( <div id="forgotpwd-form" role="form" > <h3>{trad.form_title}</h3> <p>{trad.form_desc}</p> <div className="form-group"> <label htmlFor="emailfpwd">{trad.password_label}</label> <input onChange={(password) => this.setState({passwordText:password.target.value})} type="email" id="emailfpwd" className="form-control" placeholder={trad.password_label}/> </div> <div className="form-group"> <label htmlFor="emailfpwd">{trad.password_confirmation_label}</label> <input onChange={(password) => this.setState({passwordTextConfirm:password.target.value})} type="email" id="emailfpwd" className="form-control" placeholder={trad.password_label}/> </div> <p id="expirated_reset_request" style={{display : this.state.expirated_reset_request}}>{trad.expirated_reset_request}</p> <div className="form-group"> <div className="row"> <div className="col-sm-6 col-sm-offset-3"> <button onClick={this.resetPwd.bind(this)} className="form-control btn btn-login">{trad.submit_label}</button> </div> </div> </div> </div> ); }); } resetPwd () { var validate = this.validatePassword(); var token = window.location.href.split('=').pop(); console.log(token); if(!token.match(new RegExp(/^[a-f\d.]+$/))) { alert(this.trad.message.reset_password_form[0].bad_reset_request); return false; } if(validate.success) { $.post(CONFIG.APIURL+"user/forgotpwd/"+token, {password: this.state.passwordText}, (r) => { if(r.success) { window.location = "/"+ManageCountry.getUrlRoute()+"/login"; } else { //$('#expirated_reset_request').css('display', 'block'); this.setState({expirated_reset_request : "block"}); console.log(this.state.expirated_reset_request); //'block', but in HTML, the style is always 'none' } }, 'JSON'); } } validatePassword () { if(this.state.passwordText != this.state.passwordTextConfirm) return {success : false, msg : "Les mots de passe ne correspondent pas"}; if(this.state.passwordText.length < 8) return {success : false, msg : "Le mot de passe doit faire 8 caractères au minimum"}; return {success : true}; } render = () => { return this.template; } }

我想更新一个带有表单错误的状态属性,这里是expirated_reset_request属性。

所以你可以看到,onClick执行resetPwd函数,在$ .post回调中,我正在更新状态,将显示从无(初始)更改为阻止。

之后的console.log告诉我,状态是最新的,但样式属性在HTML中永远不会改变(总是没有)

我忘记了什么吗?

提前致谢

Considering my code example (a forgot password form)

import React, { PropTypes, Component } from 'react'; import $ from 'jquery'; import ManageCountry from '../ManageCountry'; import './style.css'; import {CONFIG} from '../const.js'; export default class Forgotpwd extends Component { state = { passwordText : "", passwordTextConfirm : "", expirated_reset_request: "none" } trad = {}; constructor () { super(); ManageCountry.applyTradToState("single_sign_on", this.trad, () => { var trad = this.trad.message.reset_password_form[0]; this.template = ( <div id="forgotpwd-form" role="form" > <h3>{trad.form_title}</h3> <p>{trad.form_desc}</p> <div className="form-group"> <label htmlFor="emailfpwd">{trad.password_label}</label> <input onChange={(password) => this.setState({passwordText:password.target.value})} type="email" id="emailfpwd" className="form-control" placeholder={trad.password_label}/> </div> <div className="form-group"> <label htmlFor="emailfpwd">{trad.password_confirmation_label}</label> <input onChange={(password) => this.setState({passwordTextConfirm:password.target.value})} type="email" id="emailfpwd" className="form-control" placeholder={trad.password_label}/> </div> <p id="expirated_reset_request" style={{display : this.state.expirated_reset_request}}>{trad.expirated_reset_request}</p> <div className="form-group"> <div className="row"> <div className="col-sm-6 col-sm-offset-3"> <button onClick={this.resetPwd.bind(this)} className="form-control btn btn-login">{trad.submit_label}</button> </div> </div> </div> </div> ); }); } resetPwd () { var validate = this.validatePassword(); var token = window.location.href.split('=').pop(); console.log(token); if(!token.match(new RegExp(/^[a-f\d.]+$/))) { alert(this.trad.message.reset_password_form[0].bad_reset_request); return false; } if(validate.success) { $.post(CONFIG.APIURL+"user/forgotpwd/"+token, {password: this.state.passwordText}, (r) => { if(r.success) { window.location = "/"+ManageCountry.getUrlRoute()+"/login"; } else { //$('#expirated_reset_request').css('display', 'block'); this.setState({expirated_reset_request : "block"}); console.log(this.state.expirated_reset_request); //'block', but in HTML, the style is always 'none' } }, 'JSON'); } } validatePassword () { if(this.state.passwordText != this.state.passwordTextConfirm) return {success : false, msg : "Les mots de passe ne correspondent pas"}; if(this.state.passwordText.length < 8) return {success : false, msg : "Le mot de passe doit faire 8 caractères au minimum"}; return {success : true}; } render = () => { return this.template; } }

I want to update a state property with a form error, here the expirated_reset_request property.

So As you can see, the onClick perform the resetPwd function and in the $.post callback, I am updating the state to change the display from none (initial) to block.

The console.log just after are saying to me that state is up-to-date but the style property is never changing in the HTML (always none)

Did I forget something?

Thanks in advance

最满意答案

从我所看到的, this.template变量只填充一次。

那么,它永远不会改变。

要每次重新评估,请将其更改为函数:

this.template = () => ( <div ... )

From what I can see, the this.template variable gets populated only once.

It makes sense, then, that it never changes.

To have it re-evaluated each time, change it into a function:

this.template = () => ( <div ... )

更多推荐

本文发布于:2023-08-04 20:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422191.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是由   样式   属性   模板   React

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!