javascript - 从 componentDidMount 调用时,setstate 不会重新渲染子组件
问题描述
我有一个要设置的父组件
interface SignUpPropModel {}
interface SignUpState{
form:(InputProps&{desc: string})[];
}
export class SignUp extends Component<SignUpPropModel, SignUpState > {
state: SignUpState = {
form:[]
}
constructor(props:SignUpPropModel){
super(props)
this.state.form = [{
type:"text",
classnames:"form-control",
id: "username",
name: "username",
isvalid: false,
placeholder:"Username",
value:'',
desc:'Username'
}]
}
componentDidMount() {
let form = [...this.state.form];
if(form){
form[0].isvalid=false;
this.setState({form: form});
}
}
render(){
return (
<form className='my-3'>
{this.state.form?.map((inputProps) => (
<div key={inputProps.id} className="form-group mx-3" >
<label htmlFor={inputProps.id}>{inputProps.desc}</label>
<Input {...inputProps}/>
</div>)) }
</form>
);
}
}
输入组件 -
interface IProps{
type:'text'|'checkbox'|'radio'|'password';
value?: any;
name?: string;
id?:string;
onClick?: ((event: React.MouseEvent<HTMLInputElement, MouseEvent>) => void);
onChange?:((event: React.ChangeEvent<HTMLInputElement>) => void);
classnames: string;
placeholder?: any;
}
export interface InputProps extends IProps{
isvalid?: boolean;
}
interface InputState{
isvalid: boolean|undefined;
}
export class Input extends Component<InputProps, InputState>{
iProps: IProps|null = null;
state: InputState= {
isvalid: true
}
constructor(props: InputProps){
super(props);
({isvalid: this.state.isvalid, ...this.iProps} = this.props);
this.state.isvalid = this.state.isvalid===undefined ? true: this.props.isvalid;
}
render(){
return (
<input className={ this.state.isvalid===true ? this.iProps?.classnames : 'is-invalid' + ' ' + this.props.classnames} {...this.iProps} />
)
}
}
输入是一个子组件。它第一次渲染。在componentDidMount
os 父组件中,我正在使用setState
. 我验证了父组件的状态已更改,但不知何故render
不是由 React 触发的。因此,状态的变化不会反映在子组件中。
请帮助我找出我的代码有什么问题。
解决方案
我得到了这个问题的答案。
我假设每当 React 执行时render
,它都会重新创建组件。即我假设重新渲染将触发Input
组件的构造函数。最后,我在应用程序中进行了一些调试,发现constructor
在重新渲染时不会执行。因此,我没有state
在Input
.
发现我必须使用名为componentDidUpdate
in的生命周期方法Input
。
componentDidUpdate(){
const isvalid = this.props.isvalid===undefined ? true: this.props.isvalid;
if(this.state.isvalid !== isvalid)
this.setState({isvalid: isvalid});
}
我添加了这个,Input
问题就解决了。
推荐阅读
- rust - 调用函数返回但未分配的值的所有权会怎样?
- javascript - 无法为表中循环创建的单元格获取ElementById
- javascript - 如何在 React 中将 ArrayBuffer(PNG 图像)直接渲染为图像?
- python - 通过 xpath 查找 selenium 的元素
- swift - Xcode 自动生成的单元测试
- go - 如何为热图生成 1024 个十六进制颜色值的列表
- c# - 可以在域服务中引发域事件吗?
- c# - PayPal 账单协议 - 无效的计划 ID .NET
- ios - 执行 XCUITest 时,Tap 方法在今日视图中不起作用
- metadata - Google IoT 核心设备元数据