简体   繁体   English

在onClick事件后,React中的条件渲染无法正常工作

[英]Conditional rendering in React not working after onClick event

I was trying to conditionally show/hide a set of input fields based on a button click event. 我试图根据按钮单击事件有条件地显示/隐藏一组输入字段。 If the button was clicked, the value of showExtraDetails div will be toggled between true and false . 如果单击该按钮,则showExtraDetails div的值将在truefalse之间切换。

Here's what I've tried: 这是我尝试过的:

state = {
        ItemName: '',
        ItemPrice:'',
        ItemDate: '',
        ItemPlace: '',
        ItemType: '',
    }


/*On hitting submit button, I'm logging the state and resetting the form fields after the state was stored in to newArray after appending*/


handleSubmit = (e) => {
    e.preventDefault();
    console.log(this.state);
    newArray = [...newArray, this.state];
    console.log(newArray)
    this.setState({
        ItemName: '',
        ItemPrice:'',
        ItemDate: '',
        ItemPlace: '',
        ItemType: '',
    })
}


/*When the input fields change, I'm updating the state*/

change =e =>{
    // this.props.onChange({[e.target.name]:e.target.value})
    this.setState({
        [e.target.name]:e.target.value
    })
}

/* toggling the showExtraDetails value after button was hit */

addExtraFoodDetails=() =>{
    // e.preventDefault()
    showExtraDetails = !showExtraDetails
    console.log(showExtraDetails)
    // e.preventDefault()
}


render(){
        return(
            <div>
                <h5>Add Item</h5>
                <form onSubmit={e => this.handleSubmit(e)}>
                    <input type="text" name="ItemName" placeholder="Enter the Item name" 
                    value={this.state.ItemName}
                    onChange={e=> this.change(e)}
                    />
                    <input type="number" name="ItemPrice" placeholder="Enter the item price" 
                        value={this.state.ItemPrice}
                        onChange={e=>this.change(e)}
                    />
                    <input type="date" name="ItemDate"
                        value={this.state.ItemDate}
                        onChange={e=>this.change(e)}
                    />
                    <br/>
                    <input type="submit" name="submitItem" value="Save" />
                </form>
                <button onClick={this.addExtraDetails}>Add extra details</button>
                <br/>
                {showExtraDetails ? 
                (<div>
                    <input type="text" name="ItemPlace" value={this.state.ItemPlace}/>
                    <select name="ItemType" value={this.state.ItemType}>
                        <option value="none" disabled>--Select--</option>
                        <option value="type1">Type1</option>
                        <option value="type2">Type2</option>
                        <option value="type3">Type3</option>
                        <option value="others">Others</option>
                    </select>
                </div>) : null
                }

                <br/>
                <ul>
                    {newArray.map(item => <li>{item.ItemName}</li>)}
                </ul>
            </div>
        )
    }

But, when I hit the Add extra details button, I can only see the value of showExtraDetails as true/false after toggling, but the div is not getting updated with Showing/Hiding of the extra fields. 但是,当我点击“添加其他详细信息”按钮时,切换后我只能看到showExtraDetails的值为true/false ,但是div不会随着显示/隐藏其他字段而更新。

Am I missing something here? 我在这里想念什么吗? What is the best way to handle this? 处理此问题的最佳方法是什么? I've seen in angular ngIf would do this. 我见过ngIf可以做到这一点。 But I don't see such equivalent in React. 但是我在React中没有看到这样的效果。 I got some suggestions to make use of CSS styles by toggling the id's of the div. 通过切换div的ID,我得到了一些使用CSS样式的建议。 But I'm trying to apply something which is React specific. 但是我正在尝试应用特定于React的东西。

Thanks. 谢谢。

In all probability, react is not re rendering. 很有可能,react不会重新渲染。 I suggest putting showExtraDetails in the state, and updating this state variable to cause a re render and thus cause your component to toggle correctly. 我建议将showExtraDetails置于状态中,并更新此状态变量以引起重新渲染,从而使组件正确切换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM