【问题标题】:React: Toggle only one class without affecting the existing classReact:只切换一个类而不影响现有类
【发布时间】:2018-12-23 06:46:37
【问题描述】:

我有一个 input 字段,其中有 'from-control' 类:

<input
      name="title"
      className="form-control"
      type="text"
      placeholder="Write title..."
      value={this.state.title}
      onChange={this.onChange}
/>

现在我想切换'form-control-danger' 类而不影响'form-control' 类(当this.state.errortruefalse 时)

注意:我已经在 Google 和 StackOverflow 中搜索过,但没有找到合适的解决方案

这是我在 sn-p 中的完整代码:

class PostForm extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            title: '',
            body: '',
            fetching: false,
            error: false
        }
    }


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

    handleFormSubmit = (e) => {
        e.preventDefault()

        if (!this.state.title || !this.state.body) {
            this.setState({
                error: true
            })
            return false;
        }

        this.setState({
            fetching: true
        })

        fetch('https://jsonplaceholder.typicode.com/posts', {
            method: 'POST',
            body: JSON.stringify({
                title: this.state.title,
                body: this.state.body
            }),
            headers: {
                'Content-type': 'Application/json'
            }
        })
        .then(res => res.json())
        .then(res => {
            console.log(res)
            this.setState({
                fetching: false,
                title: '',
                body: '',
                error: false
            })
            
        })
    }

    render() {
        return (
            <div className="container">
                <div className="row justify-content-center">
                    <div className="col-8">
                        <form onSubmit={this.handleFormSubmit}>
                            <div className="form-group">
                                <label>Post Title:</label>
                                <input
                                    name="title"
                                    className="form-control"
                                    type="text"
                                    placeholder="Write title..."
                                    value={this.state.title}
                                    onChange={this.onChange}
                                />
                            </div>
                            <div className="form-group">
                                <label>Post Body:</label>
                                <textarea
                                    name="body"
                                    className="form-control"
                                    placeholder="Write body text..."
                                    value={this.state.body}
                                    onChange={this.onChange}
                                ></textarea>
                            </div>
                            <div className="form-group">
                                <button
                                    type="submit"
                                    className="btn btn-success"
                                >
                                    {
                                        this.state.fetching ? 'Fetching Post' : 'Submit'
                                    }
                                </button>
                                {this.state.error && 'title or content cannot be empty'}
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        )
    }
}

ReactDOM.render(<PostForm />, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">


<div id="app"></div>

【问题讨论】:

    标签: javascript html twitter-bootstrap reactjs


    【解决方案1】:

    这样的?

    <input
          name="title"
          className={this.state.error ? "form-control form-control-danger" : "form-control"}
          type="text"
          placeholder="Write title..."
          value={this.state.title}
          onChange={this.onChange}
    />
    

    【讨论】:

    • 我不太明白您所说的“不影响现有课程”是什么意思。会有什么不同?
    • 如果你有很多默认类,你可以这样做:className={"c1 c2 c3" + this.state.error ? " error" : ""}(注意" error" 中的空格)。或者如果你在很多地方使用它,请创建一个函数:className={this.classForInput()}.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2021-07-22
    • 1970-01-01
    • 2022-12-06
    • 2020-10-03
    • 1970-01-01
    相关资源
    最近更新 更多