38308

data flow in react application

Question:

I am currently learning react and I have run into the problem of elegently extracting states from my components.

basically I have a few components which contains forms and other inputs from which I need the data in my business logic, the data I need is coupled with the state of the component. From what I understand the data should have unidirectional flow but I can't think of how I can make my data flow back towards my business logic. I could just make some interface functions which call the respective, but I feel this would violate the unidirectional flow.

anyhelp with some examples would be greatly appreciated!

Answer1:

You typically pass down callbacks from parent components to child components as props. When the state changes in any of the child components, it invokes that callback and passes whatever data is appropriate in each use case. Your "controller-view" (the root component that implements the actual callback) then does whatever business logic you need based on the current state and then updates its state accordingly (causing a re-render of the component tree from that component down). Read the docs about <a href="http://facebook.github.io/react/tips/communicate-between-components.html" rel="nofollow">component communication</a>.

Something like this:

var Child = React.createClass({ onTextChange: function() { var val = 13; // somehow calculate new value this.props.onTextChange(val); }, render: function() { return <input type="text" value={this.props.val} onChange={this.onTextChange} /> } }); var Parent = React.createClass({ onTextChange: function(val) { var newVal = someBusinessLogic(val); this.setState({val: newVal}); }, render: function() { return <Child onTextChange={this.onTextChange} val={this.state.val} /> } });

Answer2:

The best way to work with data flow in React is to use the Flux pattern. You need some time to understand how it works, but it will make your life much easier as your project grows. Look at some Flux tutorial, for example this one (using the Alt flux implementation): <a href="https://reactjsnews.com/getting-started-with-flux/" rel="nofollow">https://reactjsnews.com/getting-started-with-flux/</a>

Recommend

  • symfony render json_array entity type and save using form
  • how to reopen a class in gems
  • Update SQL MS Access 2010
  • C++ stl pop doesn't return [closed]
  • 'Resource exhausted' memory error when trying to train a Keras model
  • When interface inheritance in Java is useful?
  • Group variable in cobol
  • iOS - Is this a task for enums?
  • R h2o.glm - issue with max_active_predictors
  • DataGridView: Pass by Value or Reference?
  • Adding independent aspx/asmx pages into DotNetNuke
  • How do you keep a running instance for Google App Engine
  • my tic-tac-toe program in matlab does not work [closed]
  • jQuery - resize an elements height to match window without refreshing, on window resize
  • xtable - background colour of added rows
  • Two Tables Serving as one Model in Rails
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Creating a DropDownList
  • Find group of records that match multiple values
  • Who propagate bugfixes across branches (corporate development)?
  • Center align outputs in ipython notebook
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Selenium to click on a javascript button corresponding to a text
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • How can I speed up CURL tasks?
  • Moving Android View and preventing onDraw to be called over and over again
  • Saving Changes After In-App Purchase Has Been Purchased
  • Custom Tabgroup Appcelerator
  • Scrapy recursive link crawler
  • Control modification in presentation layer
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Projection media query: browser support and workarounds?
  • Different response to non-authenticated users and AJAX calls
  • Javascript + PHP Encryption with pidCrypt
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Qt: Run a script BEFORE make
  • Busy indicator not showing up in wpf window [duplicate]
  • Conditional In-Line CSS for IE and Others?
  • Why do underscore prefixed variables exist?