简体   繁体   English

带有React组件的表单

[英]Forms with React components

I want to create a form with React. 我想用React创建一个表单。 I came up with the following: 我想出了以下几点:

export class Welcome extends Component {

    constructor(props) {
        super(props);
        this.state = {
            errors: []
        };
    }

    render() {
        return (
            <form className="Welcome">
                <div className="hello">{ this.props.sayHello() } I'm <Link to="/life">Marco</Link>! Welcome to my hood.</div>
                <div className="question-box">
                    <div className="question"><span className="underline" >How much time do you have?</span></div>
                    <input className="minutes" type="text" value={this.props.minutes}
                           onChange={ (e) => this.props.updatePreferences("minutes", e.target.value) }/> minutes. 
                </div>
                <div className="question-box">
                    <div className="question"><span className="underline">What do you fancy?</span></div>
                    <div className="answer">
                        <span className="choice">
                            <input type="checkbox" id="business" defaultChecked={ this.props.interests.business }/>
                            <label htmlFor="business">Business</label>
                        </span>
                        <span className="choice">
                            <input type="checkbox" id="code" defaultChecked={ this.props.interests.code }/>
                            <label htmlFor="code">Code</label>
                        </span>
                        <span className="choice">
                            <input type="checkbox" id="design" defaultChecked={ this.props.interests.design } />
                            <label htmlFor="design">Design</label>
                        </span>
                    </div>{/* end of .answer*/}
                  </div>{/* end .question-box*/}
                <button>Show me magic</button>
                <div className="errors">
                    No error. All good.
                </div>
            </form>
        );
    }
}

The onChange functions are in the parent component which also holds the state. onChange函数位于还保留状态的父组件中。 But every time they are called the whole component is reloaded. 但是每次调用它们时,整个组件都会重新加载。 Should the whole form be on a separate component or I should create separate components for each input? 整个表格应该在单独的组件上还是应该为每个输入创建单独的组件?

React call render function in response of state change. 反应呼叫渲染功能以响应状态变化。 So if component does not receive state change event it most probably will not rerendered but not necessarily. 因此,如果组件未收到状态更改事件,则很有可能不会重新渲染,但不一定。 It may rernder anyway. 无论如何,它可能会撕碎。 ref 参考

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

相关问题 Forms 作为具有反应的功能组件 - Forms as functional components with react 如何处理 React Forms 组件 - How to handle React Forms components 应对:HTML表单应该是受控制还是不受控制的组件? - REACT: Should HTML forms be Controlled or Uncontrolled components? React Forms 数据存在于两个组件中 - React Forms data is present in two components 使用 React 功能组件抽象 forms 和输入的最佳方法? - best way to abstract forms and inputs using React functional components? 使用 React-Hook-Forms 通过子组件传递方法 - Passing methods through child components using React-Hook-Forms 提交几个不同的 React forms,在管理这些 forms 的组件之外有一个按钮 - Submit several different React forms, with one button outside the components that manage those forms React AntD Forms Error元素类型无效:预期为字符串(对于内置组件) - React AntD Forms Error Element type is invalid: expected a string (for built-in components) React:如何为具有不同权限的两种用户呈现包含多个组件和 forms 的同一页面? - React: How to render same page that includes multiple components and forms for 2 types of user with different permissions? 如何使用React JS和Meteor JS在不同页面的两个表单(组件)之间传递数据? - How to pass data between two forms (components) in different pages using react js with meteor js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM