Giter Club home page Giter Club logo

Comments (6)

geek29 avatar geek29 commented on June 18, 2024

OK I read through the wiki docs and added code below and its working now.. Can you let me know if its correct way to implement companion method :

object Counter {

  def apply () = {
    val ctor = getTypedConstructor(js.constructorOf[Counter],classOf[Counter])
    createElementNoProps(ctor)
  }

}

from sri.

chandu0101 avatar chandu0101 commented on June 18, 2024

@geek29

sorry for long delay , i believe you don't need this any more! , anyway yes you're right get the ctor and pass it element factory methods. now sri has better factory methods to create elements, check out
https://github.com/chandu0101/sri/blob/master/core/src/main/scala/sri/core/ElementFactory.scala#L66

In your example

case class Props(initialCount: Int = 0)
case class State(count: Int = 0, showBorder: Boolean = true)

@ScalaJSDefined
class Counter extends ReactComponent[Props,State] {

  def border = {
    val borderStl = if(state.showBorder) "dashed" else "dotted"
    style(borderWidth := 1, borderStyle := borderStl , padding := 5)
  }
  initialState(State())
  def render() = {
    TouchableWithoutFeedback(style = border)(
      ///iew(onClick = tick _)(s"Clicks: ${state.count}")
      View(style = styles.container)(
        View(style = border)(
          Text(style = styles.text2)("Dashed border style")
        )
      )
    )
  }

  def tick():  Unit = {
    setState(state.copy(count = state.count + 1, showBorder = (!showBorder)))
  }

}
object Counter {
  def apply() = makeElement[Component]
}

you can find more examples in web-examples/mobile-examples module. :)

from sri.

nafg avatar nafg commented on June 18, 2024

@chandu0101 with the new factory there's no need for the singleton and
apply methods, you can have a top-level class called Counter and use
makeElement at the call site

On Tue, Nov 8, 2016, 5:21 AM Chandra Sekhar Kode [email protected]
wrote:

@geek29 https://github.com/geek29

sorry for long delay , i believe you don't this any more! , anyway yes
you're right get the ctor and pass it element factory methods. now sri has
better factory methods to create elements, check out

https://github.com/chandu0101/sri/blob/master/core/src/main/scala/sri/core/ElementFactory.scala#L66

In your example

object Counter {
@ScalaJSDefinedclass Component extends ReactComponent[Props,State] {

def border = {
val borderStl = if(state.showBorder) "dashed" else "dotted"
style(borderWidth := 1, borderStyle := borderStl , padding := 5)
}

initialState(State())

def render() = {
TouchableWithoutFeedback(style = border)(
///iew(onClick = tick _)(s"Clicks: ${state.count}")
View(style = styles.container)(
View(style = border)(
Text(style = styles.text2)("Dashed border style")
)
)
)
}

def tick(): Unit = {
setState(state.copy(count = state.count + 1, showBorder = (!showBorder)))
}

}case class Props(initialCount: Int = 0)case class State(count: Int = 0, showBorder: Boolean = true

)

def apply() = makeElementComponent
}


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#47 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAGAUBWxDQi81C5TH_Rbj3rbb-DDQY1mks5q8E0VgaJpZM4KFrWl
.

from sri.

chandu0101 avatar chandu0101 commented on June 18, 2024

there's no need for the singleton and
apply methods

yeah, but its more readable with singletons isn't it ?

makeElement[Counter] vs Counter()

makeElementNoPropsWithChildren[Counter]()(children) vs Counter(children)

from sri.

nafg avatar nafg commented on June 18, 2024

First of all, that's a choice users should make.
But if you want to encourage it, it would be more idiomatic to have top
level companions.

On Wed, Nov 9, 2016, 3:31 PM Chandra Sekhar Kode [email protected]
wrote:

there's no need for the singleton and
apply methods

yeah, but its more readable with singletons isn't it ?

makeElement[Counter] vs Counter()

makeElementNoPropsWithChildrenCounter(children) vs Counter(children)


You are receiving this because you commented.

Reply to this email directly, view it on GitHub
#47 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAGAUHsUresB70ZBYKj9FkJfgXpTPKKaks5q8i2PgaJpZM4KFrWl
.

from sri.

chandu0101 avatar chandu0101 commented on June 18, 2024

First of all, that's a choice users should make.

agree

But if you want to encourage it, it would be more idiomatic to have top
level companions

i feel like naming Component is more relevant in React context!

_Edit:_ yeah i am wrong Component extends ReactComponent doesn't make much sense!, i'll update the documentation thank you :)

_Edit2:_ hmm nah, when Component has Props/State/styles/etc , defining them in companion object and importing them to use inside class looks little odd! ,I still believe having a singleton is best option!. anyway its matter of style users can choose whatever they want :)

from sri.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.