简体   繁体   中英

What to use for two way binding in react js , props or state?

I am trying to create a form using reactjs , I am trying out twoway binding . I am not able to use props in the valueLink because it is can only take state .

Problem 1 : I am passing in properties when creating a component and in getInitial state I am setting it into state , as I cannot use props in valueLink . What is the better approach ?

I want to send every change in a text box to do some computation in server and get the computed values to state.

Problem 2 : I am unable to use method componentWillReceiveProps because , I use state .I am also unable use shouldComponentUpdate as I cannot do setState. What can I use here ?

valueLink doesn't work on props. You might want to try React without the two-way binding helper. The helper is really there as a convenient sugar, not as a full library feature (since React doesn't need two-way binding).

For problem 1: this is an antipattern , and another reason why you should try React without the binding helper first. Let a prop stay a prop (in most cases).

For problem 2: that is also something that's made harder by your decision of using the binding helper. As for "unable to use shouldComponentUpdate ", I'm not sure what you mean.

It seems that the value of your input is controlled higher in the hierarchy and passed down as prop. Here's what you would do without add-on: http://jsfiddle.net/TcfWe/

Note that I put the doSomeComputationOnServerSide in the parent because it feels more coherent to be alongside value 's source of truth. Depending on your needs, you might put it in the child.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM