New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How well does T3 work with ReactJS? #42
Comments
I think there are probably multiple ways, I am unsure of the best approach, you can also use services to return react components... https://gist.github.com/priyajeet/c2731789d3a688ee85a9 In this case you would need one parent module that is responsible for listening to messages via onmessage(). However all others are service subcomponents that broadcast messages via application.broadcast(). So you simulate a unidirectional data flow and render chain. The parent module does business logic (server call or whatever) and when it comes to re-rendering the UI, just calls some render function. React then takes over deciding what part of the virtual dom needs re-rendering. |
We haven't built much using T3 and React to this point, so we don't have any best practices yet. That said, we tend to think about things more in the way @priyajeet 's example shows, where a T3 module "has a" React class rather than the other way around. |
Thank you Nicholas. This helps me a lot. Hope you guys may give some best
|
We will as we learn more. Feel free to share your experiences as well. |
@nzakas I saw this in documentation that: Here, does This also brings another question: often you'll find that not all modules are presented in your html when the application starts, and instead, they will be rendered in run-time. For example, after a search, you may add a new module and render its HTML to your page, so when this happens, do I call Sorry that I have so many questions but I'd love to learn more of this framework. And Thank you very much! |
@priyajeet Thank you for the example. I think your example makes sense. It shows the core of T3. My question (the one I asked above) is: how do you ensure that after |
So when I tried using React to create the todo list, I did end up having one main bootstrapping app component outside a T3 module that did the init. I am not sure if this is the best way. var TodoApp = React.createClass({displayName: "TodoApp", componentDidMount: function() { Box.Application.init({ debug: true }); }, render: function() { return ( React.createElement("div", {"data-module": "todo-list"}) ); } }); React.render( React.createElement(TodoApp, null), document.body ); |
@priyajeet That works in this case. However what I'm not very sure about is that when you render a React component in run-time (after your application starts), then what method will you call? From the look of the document I have read, it seems you can call |
Yeah, so we do some of that stuff internally via a dom utility service. That just wraps innerHTML setting.
dom.setHTML(element, '<div data-module="todo-list"></div>')
Application.addService('dom', function(application) { return { setHTML: function(element, html) { // Safe guard against nulls and special Node objects like DocumentNode if (element && element.nodeType === 1) { application.stopAll(element); element.innerHTML = html; application.startAll(element); } else { application.reportError(new Error('setHTML() requires a valid DOM element')); } } }; }); |
@ktei |
Thank you Nicholas, that's perfect:-)
|
Here's a simple example of how to use React within a T3 module. import {Application} from 't3js';
import React from 'react';
import {render} from 'react-dom';
Application.addModule('log-in', (context) => {
class LogIn extends React.Component {
static init() {
render(<LogIn/>, context.getElement());
}
render() {
return <h1>Hello React</h1>
}
}
return LogIn;
}); |
I love ReactJS but it basically only renders View and I still need the other things like event handling, etc. It seems T3 has very simple and elegant idea. I tried a bit tonight with T3 + ReactJS. One thing I did notice is that when I run
Box.Application.init
, it expects that all the elements withdata-module
attributes are already added to Box.Application.It seems I've found that
componentWillMount
is a good place to writeBox.Application.addModule
and incomponentDidMount
, you can runBox.Application.init
.I attached my code below. Do you think this is the proper way to use T3 and ReactJS? Thank you!
The text was updated successfully, but these errors were encountered: