-
Notifications
You must be signed in to change notification settings - Fork 49
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
use of cortex where one is not in control of renderComponent #60
Comments
I'm not familiar with react-router, can you provide code of what you're trying to achieve? |
A typical usage of react router might goes as follows: var routes = (
<Route handler={App} path="/">
<DefaultRoute handler={Home} />
<Route name="about" handler={About} />
<Route name="users" handler={Users}>
<Route name="recent-users" path="recent" handler={RecentUsers} />
<Route name="user" path="/user/:userId" handler={User} />
<NotFoundRoute handler={UserRouteNotFound}/>
</Route>
<NotFoundRoute handler={NotFound}/>
<Redirect from="company" to="about" />
</Route>
);
Router.run(routes, function (Handler) {
React.render(<Handler/>, document.body);
}); You can see one is not "in control" of the React.render call. This pattern seems to be typical for other routers at well. I'm trying to use cortex and a router at the same time. What I've tried so far was to use a "wrapper" component to create the cortex object then pass it down through a prop to the "real" component. From the example above suppose I have var RealUser = React.createClass({ // the "real" component
updateFoo: function(e){
this.props.data.add(foo, e.target.value);
},
render: function(){
<input type='text' value={this.props.data.foo} onChange={this.updateFoo} />
}
});
var User = React.createClass({ // a "wrapper" component
getInitialState: function(){
var data = new Cortex({ foo: 'bar' });
data.on('update', function(newData){
this.setState({data: newData});
});
return {
data: data
}
},
componentWillUnmount: function(){
this.state.data.off('update');
},
render: function(){
return <RealUser data={this.state.data} />
}
}); Everything seems to work fine except the cursor position jumps to the end of the text field on any value change. It seems wrong to store the cortex object as state in any component but I'm not sure how to do it otherwise when I'm using a router. What I'm I missing? Surely people are using a router with react and cortex (or something similar). Thanks. |
Cortex is designed such that you can use it as either props or state, but when you use it as state then the component managing that state is in charge of changing the data. It's still unclear to me the connection between your React components and your router. Do you have |
Here's an example of the problem I'm seeing: Try typing in the text box at the left most of the text. The cursor will jump out to the right on any modification. |
I think the problem is after changing the input, your handler updates the model, which triggers react to rerender and reset the cursor position Have a look at this related issue |
OK, been going back and forth on this issue for a couple of weeks now. Here's the problem:
So the example pattern of "update state and/or props from an Cortex.on('update') callback" won't work. Unless props and/or state are updated synchronously in Finally, after event handling & rendering has concluded and Cortex's By removing I'm not sure what the best approach is to fixing this. I believe that |
This may be out of scope here but I thought I'd ask. I'm trying to react-router with cortex. One is not in control of calling render function. Is there a way of using cortex in such a fashion? The best I can come up with is a wrapper component that stores the cortex object as state and passes it down as a prop but at least one weird render problem occurs (cursor position gets lost in text fields).
Thanks
The text was updated successfully, but these errors were encountered: