{"id":133,"date":"2021-05-02T17:38:57","date_gmt":"2021-05-02T15:38:57","guid":{"rendered":"https:\/\/blog.walliczek.de\/?p=133"},"modified":"2021-05-04T20:00:33","modified_gmt":"2021-05-04T18:00:33","slug":"two-way-databinding-mit-react","status":"publish","type":"post","link":"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/","title":{"rendered":"Two-Way-Databinding mit React"},"content":{"rendered":"\n<p>Von Haus aus kann <a href=\"https:\/\/reactjs.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">React<\/a> nur One-Way-Databinding, d.h. das Update des Models muss manuell vorgenommen werden (typischweise per onChange-Handler).<\/p>\n\n\n\n<p>Gerade bei gr\u00f6\u00dferen Formularen, bei denen das Datenmodell 1:1 auch persistiert wird, entsteht dadurch viel Boiler-Plate-Code.<\/p>\n\n\n\n<p>Ich stelle hier ein Beispiel f\u00fcr eine Eingabekomponente mit Two-Way-Databinding vor, die automatisch das Modell updated. Bei einem Update des Modells in der Parent-Komponente (z.B. nach dem Laden der Daten) wird die Referenz innerhalb der Komponente aktualisiert.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class LabeledInput extends Component {\n  constructor(props) {\n    super(props);\n    \n    this.state = { value: props.value };\n    this.id = props.id;\n    if (!this.state.value&#91;this.id]) {\n      this.state.value&#91;this.id] = \"\";\n    }\n    this.handleChange = this.handleChange.bind(this);\n    this.label = props.label;\n    this.size = props.size ?? 40;\n    this.styleClass = props.styleClass;\n  }\n  handleChange(e) {\n    this.state.value&#91;this.id] = e.target.value;\n    this.setState({});\n  }\n  static getDerivedStateFromProps(props, state) {\n    if (props.value !== state.value) {\n      return {value: props.value};\n    }\n    return null;\n  }\n  render() {\n    return (\n        &lt;div className={this.styleClass + \" p-field\"}>\n          &lt;label htmlFor={this.state.value.id + this.id} className=\"p-d-block\">{this.label}&lt;\/label>\n          &lt;input type=\"text\" id={this.state.value.id + this.id} className=\"p-d-block\" size={this.size}\n              value={this.state.value&#91;this.id]} onChange={this.handleChange} \/>\n        &lt;\/div>);\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>Verwendet werden kann die Komponente wie folgt:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export class CustomerPage extends Component {\n  constructor(props) {\n    super(props);\n    this.state = { model: { } };\n    this.persist = this.persist.bind(this);\r\n  }\n  componentDidMount() {\r\n    axios.get('\/customer')\r\n    .then(response => response.data)\r\n    .then(model=> this.setState({ model}));\r\n  }\n\r  persist(e) {\n    axios.post('\/customer', this.state.model);\n  }\n  render(){\n    return (\n      &lt;div>\n        &lt;LabeledInput id=\"name\" label=\"Name\" value={this.state.model}\n \/>\n        &lt;LabeledInput id=\"email\" label=\"E-Mail-Adresse\" value={this.state.model}\n \/>\n        &lt;LabeledInput id=\"telefon\" label=\"Telefonnummer\" value={this.state.model}\n \/>\n        &lt;button onClick={this.persist}>Speichern&lt;\/button>\n      &lt;\/div>);\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Von Haus aus kann React nur One-Way-Databinding, d.h. das Update des Models muss manuell vorgenommen werden (typischweise per onChange-Handler). Gerade bei gr\u00f6\u00dferen Formularen, bei denen das Datenmodell 1:1 auch persistiert wird, entsteht dadurch viel Boiler-Plate-Code. Ich stelle hier ein Beispiel f\u00fcr eine Eingabekomponente mit Two-Way-Databinding vor, die automatisch das Modell updated. Bei einem Update des [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[23,24],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Two-Way-Databinding mit React - Matthias&#039; Technik-Ecke<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Two-Way-Databinding mit React - Matthias&#039; Technik-Ecke\" \/>\n<meta property=\"og:description\" content=\"Von Haus aus kann React nur One-Way-Databinding, d.h. das Update des Models muss manuell vorgenommen werden (typischweise per onChange-Handler). Gerade bei gr\u00f6\u00dferen Formularen, bei denen das Datenmodell 1:1 auch persistiert wird, entsteht dadurch viel Boiler-Plate-Code. Ich stelle hier ein Beispiel f\u00fcr eine Eingabekomponente mit Two-Way-Databinding vor, die automatisch das Modell updated. Bei einem Update des [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/\" \/>\n<meta property=\"og:site_name\" content=\"Matthias&#039; Technik-Ecke\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-02T15:38:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-04T18:00:33+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Verfasst von\" \/>\n\t<meta name=\"twitter:data1\" content=\"mwalliczek\" \/>\n\t<meta name=\"twitter:label2\" content=\"Gesch\u00e4tzte Lesezeit\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 Minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.walliczek.de\/#website\",\"url\":\"https:\/\/blog.walliczek.de\/\",\"name\":\"Matthias&#039; Technik-Ecke\",\"description\":\"Spielereien mit Gentoo-Linux-Servern, React, Raspberry, ESP und Co.\",\"publisher\":{\"@id\":\"https:\/\/blog.walliczek.de\/#\/schema\/person\/15d35a7cd20cd4a127d30a226f50c33f\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.walliczek.de\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"de-DE\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#webpage\",\"url\":\"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/\",\"name\":\"Two-Way-Databinding mit React - Matthias&#039; Technik-Ecke\",\"isPartOf\":{\"@id\":\"https:\/\/blog.walliczek.de\/#website\"},\"datePublished\":\"2021-05-02T15:38:57+00:00\",\"dateModified\":\"2021-05-04T18:00:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#breadcrumb\"},\"inLanguage\":\"de-DE\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.walliczek.de\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Two-Way-Databinding mit React\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#webpage\"},\"author\":{\"@id\":\"https:\/\/blog.walliczek.de\/#\/schema\/person\/15d35a7cd20cd4a127d30a226f50c33f\"},\"headline\":\"Two-Way-Databinding mit React\",\"datePublished\":\"2021-05-02T15:38:57+00:00\",\"dateModified\":\"2021-05-04T18:00:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#webpage\"},\"wordCount\":85,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blog.walliczek.de\/#\/schema\/person\/15d35a7cd20cd4a127d30a226f50c33f\"},\"keywords\":[\"React\",\"Two-Way-Databinding\"],\"articleSection\":[\"React\"],\"inLanguage\":\"de-DE\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/blog.walliczek.de\/#\/schema\/person\/15d35a7cd20cd4a127d30a226f50c33f\",\"name\":\"mwalliczek\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/blog.walliczek.de\/#personlogo\",\"inLanguage\":\"de-DE\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5920ffa7140bf7a615c7d7f0c3fd9b2c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5920ffa7140bf7a615c7d7f0c3fd9b2c?s=96&d=mm&r=g\",\"caption\":\"mwalliczek\"},\"logo\":{\"@id\":\"https:\/\/blog.walliczek.de\/#personlogo\"},\"sameAs\":[\"https:\/\/blog.walliczek.de\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Two-Way-Databinding mit React - Matthias&#039; Technik-Ecke","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/","og_locale":"de_DE","og_type":"article","og_title":"Two-Way-Databinding mit React - Matthias&#039; Technik-Ecke","og_description":"Von Haus aus kann React nur One-Way-Databinding, d.h. das Update des Models muss manuell vorgenommen werden (typischweise per onChange-Handler). Gerade bei gr\u00f6\u00dferen Formularen, bei denen das Datenmodell 1:1 auch persistiert wird, entsteht dadurch viel Boiler-Plate-Code. Ich stelle hier ein Beispiel f\u00fcr eine Eingabekomponente mit Two-Way-Databinding vor, die automatisch das Modell updated. Bei einem Update des [&hellip;]","og_url":"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/","og_site_name":"Matthias&#039; Technik-Ecke","article_published_time":"2021-05-02T15:38:57+00:00","article_modified_time":"2021-05-04T18:00:33+00:00","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"mwalliczek","Gesch\u00e4tzte Lesezeit":"1 Minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/blog.walliczek.de\/#website","url":"https:\/\/blog.walliczek.de\/","name":"Matthias&#039; Technik-Ecke","description":"Spielereien mit Gentoo-Linux-Servern, React, Raspberry, ESP und Co.","publisher":{"@id":"https:\/\/blog.walliczek.de\/#\/schema\/person\/15d35a7cd20cd4a127d30a226f50c33f"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.walliczek.de\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"de-DE"},{"@type":"WebPage","@id":"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#webpage","url":"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/","name":"Two-Way-Databinding mit React - Matthias&#039; Technik-Ecke","isPartOf":{"@id":"https:\/\/blog.walliczek.de\/#website"},"datePublished":"2021-05-02T15:38:57+00:00","dateModified":"2021-05-04T18:00:33+00:00","breadcrumb":{"@id":"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#breadcrumb"},"inLanguage":"de-DE","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.walliczek.de\/"},{"@type":"ListItem","position":2,"name":"Two-Way-Databinding mit React"}]},{"@type":"Article","@id":"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#article","isPartOf":{"@id":"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#webpage"},"author":{"@id":"https:\/\/blog.walliczek.de\/#\/schema\/person\/15d35a7cd20cd4a127d30a226f50c33f"},"headline":"Two-Way-Databinding mit React","datePublished":"2021-05-02T15:38:57+00:00","dateModified":"2021-05-04T18:00:33+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#webpage"},"wordCount":85,"commentCount":0,"publisher":{"@id":"https:\/\/blog.walliczek.de\/#\/schema\/person\/15d35a7cd20cd4a127d30a226f50c33f"},"keywords":["React","Two-Way-Databinding"],"articleSection":["React"],"inLanguage":"de-DE","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.walliczek.de\/index.php\/2021\/05\/02\/two-way-databinding-mit-react\/#respond"]}]},{"@type":["Person","Organization"],"@id":"https:\/\/blog.walliczek.de\/#\/schema\/person\/15d35a7cd20cd4a127d30a226f50c33f","name":"mwalliczek","image":{"@type":"ImageObject","@id":"https:\/\/blog.walliczek.de\/#personlogo","inLanguage":"de-DE","url":"https:\/\/secure.gravatar.com\/avatar\/5920ffa7140bf7a615c7d7f0c3fd9b2c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5920ffa7140bf7a615c7d7f0c3fd9b2c?s=96&d=mm&r=g","caption":"mwalliczek"},"logo":{"@id":"https:\/\/blog.walliczek.de\/#personlogo"},"sameAs":["https:\/\/blog.walliczek.de"]}]}},"_links":{"self":[{"href":"https:\/\/blog.walliczek.de\/index.php\/wp-json\/wp\/v2\/posts\/133"}],"collection":[{"href":"https:\/\/blog.walliczek.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.walliczek.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.walliczek.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.walliczek.de\/index.php\/wp-json\/wp\/v2\/comments?post=133"}],"version-history":[{"count":4,"href":"https:\/\/blog.walliczek.de\/index.php\/wp-json\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":145,"href":"https:\/\/blog.walliczek.de\/index.php\/wp-json\/wp\/v2\/posts\/133\/revisions\/145"}],"wp:attachment":[{"href":"https:\/\/blog.walliczek.de\/index.php\/wp-json\/wp\/v2\/media?parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.walliczek.de\/index.php\/wp-json\/wp\/v2\/categories?post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.walliczek.de\/index.php\/wp-json\/wp\/v2\/tags?post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}