xored / scala-js-react

ScalaJS interface for Facebook React
Apache License 2.0
133 stars 13 forks source link

Missing setProps in ReactComponent #8

Closed sergiusignacius closed 9 years ago

sergiusignacius commented 9 years ago

Hi! First of all, great job on implementing this port of React, it's really useful. :)

I was wondering: is there a setProps method missing in the current version, or if there is an alternative way to achieve the same result as calling it in the library?

Thanks, Sérgio

kanterov commented 9 years ago

Hi Sérgio,

Glad to hear that :+1:.

I didn't add it because React docs say that the best way to do this is to call renderComponent once again with updated props.

http://facebook.github.io/react/docs/component-api.html#setprops

At Xored we have pretty large codebase using scala-js-react and didn't find any limitations of this approach. Let me know if it works for you.

Thanks, Gleb

sergiusignacius commented 9 years ago

Hi! It does: calling renderComponent again works. But my struggle is understanding how to do it nicely. Example: I have a Table component I'm rendering with a list of values (a property called data), like this:

React.renderComponent( CitiesTable(CitiesTable.Props(headers=List("City", "State", "Population"), data=cities.value)), document.getElementById("content"))

When the list of values changes I want to re-render the Table. What is the right way to do it? Repeat the exact same expression as before? What can be refactored?

Thanks again, Sérgio

kanterov commented 9 years ago

Hi Sérgio,

Sorry for the delay.

As I know, in React there are following ways to re-render top-level component:

So I guess you have a function with callback that is triggered when your data changes. You can wrap a call of this function into a component componentDidMount and then do setState in callback. Your render method would render CitiesTable passing component state into it's props.

Thanks, Gleb

kanterov commented 9 years ago

@sergiusignacius did it work for you?

kanterov commented 9 years ago

Closing due to reporter inactivity.