- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个组件,在该组件中我想显示取决于 props
值的状态。我总是遇到错误并且无法在网上找到好的示例。
这是我的组件(如下),我想在 this.props.passRecoveryEditSuccess.password_changed
等于 1 时显示
this.state.msg
值
我编写了 massage
函数,该函数应该根据从 reducer 返回的内容来设置 msg
的状态。
如何解决这个问题?
`
import React, { Component } from 'react';
import {Form, Alert, FormGroup, Col, Button, FormControl, Grid} from 'react-bootstrap';
import {connect} from 'react-redux';
import {Link} from 'react-router-dom';
import Header from '../header/Header';
import { PassRecoveryEdit } from '../../actions/ajax';
class PasswordRecoveryEdit extends Component {
constructor(props){
super(props);
this.state ={
password: '',
confirmPassword: '',
msg: ''
}
}
componentDidUpdate(){
if(this.props.passRecoveryEditSuccess){
this.massage()
}
}
onChange = (e) => {
this.setState({
[e.target.name]: e.target.value
})
}
formValidation = () => {
let isError = false;
const errors = {};
if(this.state.password.length < 6){
isError = true;
errors.passwordLengthError = "על הסיסמה להיות ארוכה מ 6 תווים"
}else{
isError = false;
errors.passwordLengthError = ""
}
if( this.state.confirmPassword !== this.state.password){
isError = true;
errors.passwordMatchError = "הסיסמאות לא תואמות"
}else{
isError = false;
errors.passwordMatchError = ""
}
this.setState({
...this.state,
...errors
});
return isError
}
onSubmit = (e) => {
e.preventDefault();
//console.log('onSubmit');
//console.log('this.state', this.state);
let obj = {
password: this.state.password,
token: this.props.match.params.token
}
let err = this.formValidation();
if(!err){
this.props.PassRecoveryEdit(obj);
// console.log('success');
}
}
massage = () => {
if(this.props.passRecoveryEditSuccess.password_changed == 1){
this.setState({msg: 'הסיסמה עודכנה בהצלחה '});
}else{
this.setState({msg: 'הסיסמה לא עודכנה בהצלחה'});
}
}
render() {
return (
<div>
<Header headline="הזינו סיסמה חדשה"/>
<Grid>
<Form horizontal onSubmit={this.onSubmit.bind(this)}>
<FormGroup>
<FormControl
ref="password"
name="password"
id="password"
type="password"
placeholder="הקלידו את הסיסמה"
aria-label="הקלידו את הסיסמה"
onChange={this.onChange.bind(this)}
/>
</FormGroup>
{this.state.passwordLengthError &&
<Alert variant="danger" className="text-right">
{this.state.passwordLengthError}
</Alert>
}
<FormGroup>
<FormControl
ref="confirmPassword"
name="confirmPassword"
id="confirmPassword"
type="password"
placeholder="הקלידו הסיסמה שנית"
aria-label="הקלידו הסיסמה שנית"
onChange={this.onChange.bind(this)}
/>
</FormGroup>
{this.state.passwordMatchError &&
<Alert variant="danger" className="text-right">
{this.state.passwordMatchError}
</Alert>
}
<FormGroup>
<Col xs={12}>
<Button className="full-width-btn" type="submit">שינוי סיסמה</Button>
</Col>
</FormGroup>
</Form>
{this.state.msg &&
<Alert variant="danger" className="text-right">
{this.state.msg}
</Alert>
}
</Grid>
</div>
)
}
}
const mapStateToProps = state => {
return{
passRecoveryEditSuccess: state.userReducer.passRecoveryEdit
}
}
export default connect(mapStateToProps, {PassRecoveryEdit})(PasswordRecoveryEdit)
`
最佳答案
您正在创建一个无限循环。每当组件中的状态或属性发生变化时,componentDidUpdate()
就会被调用。因此,在第一次渲染之后,您从 redux 获取更新的 props,因此 componentDidUpdate()
触发,条件通过,然后您调用 this.massage()
然后 this.massage()
更新组件的状态,重新触发 componentDidUpdate()
,它在调用 之前检查完全相同的条件this.massage()
,从而创建循环。
您可以做的是在 componentDidUpdate()
中获取 prevProps
参数,并使用它来创建更 protected 条件。
componentDidUpdate(prevProps){
if(this.props.passRecoveryEditSuccess !== prevProps.passRecoveryEditSuccess){
this.massage()
}
}
这样,您就表示只有当属于前一个渲染的 props 与新渲染不相等时,您才会调用 this.massage()
。
关于javascript - React js-显示状态值取决于 redux props 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57903078/
在具有资源的Grails 2.3X的旧世界中,可以按特定顺序定义实际资源,即dependsOn 我一直在寻找 Assets 说明: https://github.com/bertramdev/asse
嗨,我正处于一个场景中,我正在考虑拥有多个模块,并让模块依赖于其他模块。 假设我们正在构建一个业务系统,其中一部分是进销存,并且我们在不同的分支机构都有客户。 因此,一个行业可能希望使用某些工具并以自
#include int main(void) { char ch; int count; while((ch=getchar())!=EOF){ if(ch
我想使用基于 gcc 开关的 #if 指令交叉编译特定的 32 位和 64 位代码。 我可以使用 GCC 设置的任何宏来代替使用 -D 开关设置宏吗? 我能否以某种方式获取 gcc 命令选项中使用的开
这是对我之前的 question 的更复杂的后续操作.答案是使用矩阵,但这不适用于具有不同模式值的数据框。 我想将不同大小的数据框与字符和整数列组合在一起,并根据多个条件计算它们的总和。 状况 仅针对
希望获得有关 dependsOn 的一些指导和解释。 我在模板中看到有两种在模板中提供依赖项的方法。 一种方法是提供resourceId,另一种方法是使用concat提供字符串值。我试图理解两者之间的
我编写了一些包含一些额外函数的包,用于处理反射(reflect)我喜欢使用它们的方式的常见类型(例如,我更喜欢将 Bson 文档视为 map ,而不是字段列表)。我将这些包保存在本地源目录中,而不是在
我有类似于这样的 View 模型: public class MyDialogViewModel { public MyButtonViewModel[] Buttons{ get; }
我只使用一项 Activity ,很少使用碎片。我只想在启动画面上隐藏工具栏。我这样写: class MainActivity : AppCompatActivity() { lateinit var
在我的 M-V-VM 应用程序中,我必须显示用户的头像。图像在 ViewModel 对象的 ImageSource 类型的属性中提供。这就是我目前拥有的: 但是,有些用户可能没有配置头像,所以Use
如何在 setTimeout 后调用带有 addEventListener 的函数?这是代码示例: xyz = setTimeout(function () { [...blabla func
我正在学习 Spring 并进行一些研发。 我尝试了“依赖”概念。 sparsh --> 25 3
我有一个 Rust 库,它基本上是一些 C 源代码和一个 bindgen 包装器。它有一个进入 C 源目录的 build.rs 脚本,运行 make,并将 .a 静态库复制到 $OUT_DIR。当我运
我使用 middleman用于开发网站并希望使用 .yml 文件将标记与内容分开。那么问题来了: 有没有办法根据某个 css 类更改 ruby 数据路径? 例如,我只想在父标签中有一个 css 类
此代码在转到 URL 的目标之前淡出每个页面。但是,在某些情况下,用户不会转到新页面,而是转到浏览器中的 PDF,或者打开默认邮件应用程序。在 Safari 上,如果您转到外部站点 (www.twit
在 Microsoft Solver Foundation 中,我想知道是否可以添加一个值取决于决策值的参数。 即我想要 TSP 模型的一些东西,但它还应该考虑从一个点到另一个点的流量。请注意:流量取
我正在尝试根据来自同一张表的 cid 获取新表中成人和 child 的总和。 餐 table 销售 +-----------+------------+---------------+ | cid
我有这个 .htaccess 文件,但我希望它只在我在实时站点上时才这样做。 有什么办法可以: // if server_name looks like example.com to this //
我对编程很陌生,现在一直在寻找解决方案,但由于我不确定我到底在寻找什么,所以我决定问你。 根据 Firebase 上的数据,我希望构建一个包含文本和图像的按钮。 例如,我有一个数据库,包括: User
我正在网上商店运行一个表单,如下所示: 按下提交按钮后,我根据从 $_POST 获得的信息在 php 中运行一个简单的错误检查。我有一个名为 $errors 的数组,如果我
我是一名优秀的程序员,十分优秀!