frank
06/21/2020, 8:44 PMonSendMessageClicked()
function inside of my functionalComponent the elements can't find it and I need to declare it outside.
val chatWindowComponent = functionalComponent<ChatMsgListProps>{ props ->
val (mensajes,setMensajes) = useState(arrayOf<ReactElement>())
div(classes = "chatWindow") {
div {
button(classes = "sendMessageButton") {
+"Send Message"
attrs.type = ButtonType.button
attrs.onClickFunction = onSendMessageClicked()
}
}
}
fun onSendMessageClicked(): (Event) -> Unit {
return {
//Code
}
}
}
The disadvantage of declaring onSendMessageClicked()
outside is that I need to send setState
in all functions that use it. Alternatives to this?
fun onSendMessageClicked(setMsg: RSetState<Array<ReactElement>>): (Event) -> Unit {
return {
//Something(setMsg)
}
}
frank
06/21/2020, 11:29 PMRbuilder
but I dont know if it is bad Practices. Alternatives to this?
fun RBuilder.onSendMessageClicked(): (Event) -> Unit {
return {
//Code
}
}
andylamax
06/22/2020, 4:20 AMChatMsgListProps
that way, it can be passed down as a prop, instead of being able to be invoked from any RBuilder
frank
06/22/2020, 4:56 AMonSendMessageClicked()
function before of ReactElement for find it in call.
`onSendMessageClicked`should be a function variable inside@andylamax, I dont understand you if i declare onclick variable inside of my Props. Where define my onclick function and how call it? Do you have any sample?that way, it can be passed down as a prop, instead of being able to be invoked from anyChatMsgListProps
RBuilder
andylamax
06/22/2020, 5:24 AMexternal interface ChatMsgListProps {
var onMessageSent:()->Unit
}
val ChatWindowComponent = functionalComponent<ChatMsgListProps> { props->
div(classes = "chatWindow") {
div {
button(classes = "sendMessageButton") {
+"Send Message"
attrs.type = ButtonType.button
attrs.onClickFunction = {props.onMessageSent()}
}
}
}
}
Then after that, you pass your handler at invocation site, something like
child(ChatWindowComponent,jsObject<ChatMsgListProps>{}) {
attrs.onMessageSent = { /*Handle your call back here*/}
}
frank
06/22/2020, 12:03 PMfun onSendMessageClicked(setMsg: RSetState<Array<ReactElement>>): (Event) -> Unit {
return {
//Something(setMsg)
}
}
andylamax
06/22/2020, 12:42 PMReactElement
as a state? That is a red flag.
2. Sending down state has been the react way until the introduction of the Contex
use React.Context
to minimize prop drilling.
3. Do your best to avoid `ReactComponent`'s or `ReactElement`'s in your state. Your state shouldn't contain any UI
element. Just the model, that the UI
can renderfrank
06/22/2020, 1:39 PMWhy do you haveMy Project is chat app and the ReactElement is dynamic list of messages. I need it to update every time I append a message in my list. looking for info in google was the only way I found.as a state? That is a red flag.ReactElement
Sending down state has been the react way until the introduction of thei will try ituseContex
to minimize prop drilling.React.Context
andylamax
06/22/2020, 1:43 PMReactElement
, your Component
should take an array/list of messages and render a list of ReactElement
on its ownfrank
06/23/2020, 6:01 AMyou should have a list of messages. Not a list ofIf I have seen it, I made a mess with ReactElement I found a good sample of a chat app with react, I will follow it. @andylamax, You say:ReactElement
Do your best to avoid ReactElement's in your state.
Have you had problems in any app? I have tried it with some examples in react and I have not had problems with React, with Kotlin-react if I had problems.andylamax
06/23/2020, 6:19 AMui = fun(model)
. When your components
have other other components
as state
you will have a hard time scaling in a large application where you need to hoist your state up, and thats going to be a problem. Because you are going to have child components
on your hoisted states.
states are for holding data not ui elements
This appears in basically all frontend frameworks.
Have you had problems in any app?
Yes. When I had no deeper knowledge of states and props, I would stack some elements in my state. It became way harder to maintain my own code. After digging a lot, that is when I realized that paradigm.frank
06/24/2020, 6:46 AMarrow functions
, data-attributes
or sub-components
?
The performance problems of the Arrow functions
are usually serious?andylamax
06/24/2020, 7:32 AMfrank
06/24/2020, 10:41 AMshould be a function variable inside ChatMsgListProps that way, it can be passed down as a prop
and I had a question about the functions with params.frank
06/24/2020, 11:36 AMandylamax
06/29/2020, 10:33 PM