[英]react set state not rerendering component
When i use a ternary expression in react render method it renders the false component fine.当我在反应渲染方法中使用三元表达式时,它会很好地渲染错误组件。 when set to true nothing happens however when i log state it's updated with true.当设置为 true 时,没有任何反应,但是当我登录 state 时,它已更新为 true。 so this is really confusing.所以这真的很令人困惑。 i even tried it with ternary string return and that worked fine, its just the component that wont work.我什至用三元字符串返回试过它并且工作正常,它只是无法工作的组件。 I also found that it renders both when i wrap FormGroup in a empty div.我还发现,当我将 FormGroup 包装在一个空的 div 中时,它会呈现两者。 this is really weird.这真的很奇怪。 What's supposed to happen is when i click the radio button it will set state to true and variantForm will be true having the ternary return a diff component.应该发生的是,当我单击单选按钮时,它会将 state 设置为 true,而 variantForm 将为 true,使三元返回 diff 组件。
my state on init我的 state 初始化
{variantForm: false}
after radio button clicked to true单选按钮点击为真后
{variantForm: true}
component did mount组件确实挂载
this.productForm.current.controls.hasVariants.current.valueChanges().subscribe(boolean=>{
this.setState({variantForm:boolean},()=>{
console.log(this.state);
})
})
inside render method内部渲染方法
<div className="dashboard-body">
<p>{this.state.variantForm? 'true':'false'}</p> // works
<FormGroup ref={this.productForm} groupName="productForm">
<FormControl element={InputField} fieldName="productName" label="Product Name"></FormControl>
<FormControl defaultValue={false} element={RadioGroupField} fieldName="hasVariants" label="Has Variants"></FormControl>
</FormGroup>
{this.state.variantForm? <FormGroup ref={this.productLowerForm} groupName="variantForm">
<FormControl element={InputField} fieldName="variantName" label="Variant Name"></FormControl>
</FormGroup> :
<FormGroup ref={this.productLowerForm} groupName="productLowerForm">
<FormControl element={InputField} fieldName="productName" label="Product Name"></FormControl>
<FormControl element={InputField} fieldName="bin" label="Bin"></FormControl>
<FormControl element={InputField} fieldName="quantity" label="Quantity"></FormControl>
</FormGroup>
}
</div>
what worked but not sure why was什么有效但不确定为什么
{this.state.variantForm?
<div><FormGroup ref={this.productLowerForm} groupName="variantForm">
<FormControl element={InputField} fieldName="variantName" label="Variant Name"></FormControl>
</FormGroup></div> :
<FormGroup ref={this.productLowerForm} groupName="productLowerForm">
<FormControl element={InputField} fieldName="productName" label="Product Name"></FormControl>
<FormControl element={InputField} fieldName="bin" label="Bin"></FormControl>
<FormControl element={InputField} fieldName="quantity" label="Quantity"></FormControl>
</FormGroup>
}
if your wondering what FormGroup returns its the react fragment because no container was provided如果你想知道什么 FormGroup 返回它的反应片段,因为没有提供容器
<div className="formGroup">
{this.props.container ? <this.props.container>{this.clonedChildren}</this.props.container> : <React.Fragment>{this.clonedChildren}</React.Fragment>}
</div>)
this also works?这也有效?
{this.state.variantForm === true?
<FormGroup ref={this.productLowerForm} groupName="variantForm">
<FormControl element={InputField} fieldName="variantName" label="Variant Name"></FormControl>
</FormGroup> : null}
{this.state.variantForm === false?
<FormGroup ref={this.productLowerForm} groupName="productLowerForm">
<FormControl element={InputField} fieldName="productName" label="Product Name"></FormControl>
<FormControl element={InputField} fieldName="bin" label="Bin"></FormControl>
<FormControl element={InputField} fieldName="quantity" label="Quantity"></FormControl>
</FormGroup>: null
}
I realized my issue was because the radio button was not a boolean it was a string.我意识到我的问题是因为单选按钮不是 boolean 它是一个字符串。 so it was always true.所以它总是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.