score:206
angular
when angular sets up databinding two "watchers" exist (this is a simplification)
//js
$scope.name = 'test';
$timeout(function() { $scope.name = 'another' }, 1000);
$timeout(function() { console.log($scope.name); }, 5000);
<!-- html --->
<input ng-model="name" />
the input will start out with test
, then update to another
in 1000ms. any changes to $scope.name
, either from controller code or by changing the input, will be reflected in the console log 4000ms later. changes to the <input />
are reflected in the $scope.name
property automatically, since ng-model
sets up watches the input and notifies $scope
of the changes. changes from the code and changes from the html are two-way binding. (check out this fiddle)
react
react doesn't have a mechanism to allow the html to change the component. the html can only raise events that the component responds to. the typical example is by using onchange
.
//js
render() {
return <input value={this.state.value} onchange={this.handlechange} />
}
handlechange(e) {
this.setstate({value: e.target.value});
}
the value of the <input />
is controlled entirely by the render
function. the only way to update this value is from the component itself, which is done by attaching an onchange
event to the <input />
which sets this.state.value
to with the react component method setstate
. the <input />
does not have direct access to the components state, and so it cannot make changes. this is one-way binding. (check out this codepen)
score:0
one way data binding is very simple, except in react we rarely use the word binding to refer how the data flows.
const fn = (a) => { return ... }
if a value is provided as a
, we'll use that value in the function scope. the above is programming 101.
<title name={"hello"} />
the above line doesn't mean anything would magically happen other than the fact that "hello" is sent to title function and set one prop to "hello", if you insist to use the word bind here, that's where the bind happens.
whether you want to use this prop to display or wire with another state or whatever, you have to code yourself! there's no other magic. btw, this is called props in react. and props is more or less a function's input argument coded in an object format. so the more accurate definition of this "bind" in react should be called assignment. in react source code, you'll see something very quickly after the element is created.
element.props = { name: "hello" }
and believe it or not, there's no other code in react that does anything to do with this "bind" later on.
example
use the input example,
<input value={value} onchange={onchange} />
if we give a value
to input
, the input would pick up the value to display it. if you change the value, you are intending to change the display.
why does the value
change? it can't by default. you have to change it by either listening to a system event like onchange
or some business logic like settimeout
or whatever way you can ever imagine. but the change is an action, you perform the action therefore you can handle the action by changing the value. i guess this is where the "one-way" comes from. essentially nothing is free.
confusion
what gets us confused is that dom element has its own state or properties. for example, we can do el.textcontent="abc"
without using react.
<input />
if we just code like this, we still see the value on screen changes after we type in anything. but this behavior has nothing to do with react, it's the dom element functionalities. react refers to the first version as controlled element. essentially react overwrites the dom way.
note
to be honest, only after i stopped using the word "binding" for these cases, i started to understand what they are. but that could be just me.
score:2
what is data binding?
data binding is a general technique that binds data sources from the provider and consumer together and synchronizes them.
data biding in angular
according to angularjs documents, data-binding in angularjs apps is the automatic synchronization of data between the model and view components. the view is a projection of the model at all times. when the model changes, the view reflects the change, and vice versa
the template (which is the uncompiled html along with any additional markup or directives) is compiled on the browser. the compilation step produces a live view. any changes to the view are immediately reflected in the model, and any changes in the model are propagated to the view.
data binding in reactjs
the connection between the data to be displayed in the view and the component’s logic is called data binding in reactjs. reactjs uses one-way data binding. in one-way data binding one of the following conditions can be followed:
- component to view: any change in component data would get reflected in the view.
- view to component: any change in view would get reflected in the component’s data.
score:15
two-way data binding provides the ability to take the value of a property and display it on the view while also having an input to automatically update the value in the model. you could, for example, show the property "task" on the view and bind the textbox value to that same property. so, if the user updates the value of the textbox the view will automatically update and the value of this parameter will also be updated in the controller. in contrast, one way binding only binds the value of the model to the view and does not have an additional watcher to determine if the value in the view has been changed by the user.
regarding react.js, it was not really designed for two way data binding, however, you can still implement two-way binding manually by adding some additional logic, see for example this link. in angular.js two-way binding is a first class citizen, so there is no need to add this additional logic.
Source: stackoverflow.com
Related Query
- Can anyone explain the difference between Reacts one-way data binding and Angular's two-way data binding
- What's the crucial difference between Angular 2 Data Flow and Flux?
- What's the Difference between Remark & Rehype, and why would I want to use one over the other?
- What is the difference between passing data in this way to properties in React?
- What is the correct way of setting state variables in Reactjs and what is the difference between these approaches?
- How would one do two way data binding with react, having an input value A update when B is, and vice versa?
- What is the difference between func with parentheses and one without it
- Can someone explain me the difference between both this if conditions in React?
- Not getting data from api into the web page, can anyone help me through this where I am getting an error and what can i do to get data
- Can someone explain to me why one function works and the other don't?
- Can someone explain to me what the correct way to pass the data is?
- What is the difference between React functional component <Message id> and js function formatMessage() and which one is better to use?
- Can anyone explain why this returns between 50 to 100 of the same values? I am using this to change what a function does at a certain window width
- What is the difference between these two arrays, and how can I access the properties i need?
- how can i get the more than one data row value based on selected checkbox and store in useState (react-table-boostrap2)
- ReactJs : What is the proper way of accessing the state data in the root component where the link between React and Redux has been implemented?
- What is the difference between React Native and React?
- What is the difference between state and props in React?
- What's the difference between "super()" and "super(props)" in React when using es6 classes?
- What's the difference between `useRef` and `createRef`?
- What is the difference between HashRouter and BrowserRouter in React?
- What's the difference between useCallback and useMemo in practice?
- What is the difference between .ts and .tsx extensions. Both are used as extensions for typescript files in react. So where should we use them?
- In React, what's the difference between onChange and onInput?
- In useEffect, what's the difference between providing no dependency array and an empty one?
- What's the difference between hydrate() and render() in React 16?
- What is the difference between redux-thunk and redux-promise?
- What is the difference between componentWillMount and componentDidMount in ReactJS?
- What is the difference between NextJs and Create React App
- What is the difference between jest.fn() and jest.spyOn() methods in jest?
More Query from same tag
- input allows to write 1 symbol only
- how to pass an event from a child component to parent component back down to another child in React
- React testing library getting element
- React app, API call that fetches routes for app
- How do I change the theme in an AnyChart React tag cloud?
- Using React Redux, when click the link on the side bar (to redirect) to clear the input in the search bar at the same time
- 'npm run build' often freezes my entire server
- Allow input type number only a number/float between 0-1
- How to add extra onClick handler along with the user's onClick on the button in react?
- Not able to show fetched data from firebase into reactjs webapp
- Confusion about bootstrap Cards with React
- filter array using includes
- Why does the mui datepicker onChange method revert the format of the date back to the default format
- React Component Function not working onClick button
- How to use React TransitionMotion willEnter()
- Creating a label alongside an input using material-ui's TextField and getInputProps
- React Material UI can't find mobile device on first render
- Initializing the Container with API call unstated.js (React Context API)
- Changing a nested array value based on a given index in a reducer while not mutating state
- webpack and react jsx - hot loading not working with docker container
- How to change background color of active Navigation Tab?
- Login Error: Error: A case reducer on a non-draftable value must not return undefined
- Get type of sibling parameter
- TypeError: Cannot read property 'attach' of undefined makeStyles.js
- Get react state with dynamic key
- React components not rendering on browser but not showing errors
- how to call function inside reactjs render method properly?
- NextAuth.js for authentication with Google facing [client_fetch_error] with next.js
- How to send a form input data containing both image and text from React front-end to Express backend using Multer
- Best way to use classes and styles in a react website