![]() Component, 1000 ) Alternatives to Controlled Components An input form element whose value is controlled by React in this way is called a “controlled component”.įor example, if we want to make the previous example log the name when it is submitted, we can write the form as a controlled component:Ĭlass NameForm extends React. Then the React component that renders a form also controls what happens in that form on subsequent user input. We can combine the two by making the React state be the “single source of truth”. In React, mutable state is typically kept in the state property of components, and only updated with setState(). In HTML, form elements such as, , and typically maintain their own state and update it based on user input. ![]() The standard way to achieve this is with a technique called “controlled components”. But in most cases, it’s convenient to have a JavaScript function that handles the submission of the form and has access to the data that the user entered into the form. ![]() ![]() If you want this behavior in React, it just works. This form has the default HTML form behavior of browsing to a new page when the user submits the form. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |