Comments (16)
Hi! Thanks for the kind words.
- This is really up to you. The traditional Flux approach is to use a single container, and generally that's the way to go, but because Flummox does not use singletons, there could be cases where you need or want a local Flux class. v3.0 will make this easier by allowing you to customize the name of FluxComponent's
flux
prop to avoid clashes. #60 - Yes, and that's why you generally want to use a single Flux instance. I would say only use separate instances if they have completely isolated concerns. You could use actions communicate between Flux instances, but at that point you should probably just combine them.
Hope that helps!
from flummox.
To clarify, the main purpose of Flux's non-singleton, everything-is-an-instance approach is to enable server-side rendering. It just also happens to have other benefits, like the ability for local Flux. But the recommended approach is still for a single, "global" Flux class.
from flummox.
- Yep, saw the roadmap ;)
- Ok that makes sense. Just one question though: if I have only 1 instance of
Flux
, every action / store created in it will be referenced in the container (_stores
,_actions
). So we might have a big fat object here that we ideally pass through all our React components. Are there any bad consequences about that or something we need to be careful?
from flummox.
JavaScript passes objects by reference, so the fatness of the object makes no difference. Unless I misunderstood your question, which is possible :)
from flummox.
Yeah, just wondering if it's ok to pass those "complex" objects as props
(it's a question more related to React I guess).
Also, would you recommend to pass the flux
instance directly to React context or use props
? Are there any drawbacks or does it make no difference at all?
from flummox.
Using props is more explicit but it doesn't scale well. I suggest using FluxComponent:
// entry point
// this adds flux to context
React.render(
<FluxComponent flux={flux}>
<App />
</Flux>
, el)
// elsewhere, use FluxComponent to access flux
<FluxComponent>
<Something /> // has flux prop
</FluxComponent>
from flummox.
Yes, I meant this as props
;)
The other option I saw somewhere was by using React.withContext
.
But yeah, I guess I can just wrap my App
component in a FluxComponent
and inject flux
there.
👍
from flummox.
React.withContext()
is deprecated in 0.13 https://github.com/facebook/react/blob/master/src/core/ReactContext.js#L54
from flummox.
Perfect, thanks for the help 👍
from flummox.
@acdlite hey sorry to bother you again. I've adjusted my example by wrapping a FluxComponent
and got a weird behaviour. Let me show you:
import Dispatcher from './Dispatcher'
let dispatcher = new Dispatcher()
let Application = React.createClass({
render() {
return (
<FluxComponent flux={dispatcher}>
<div>
<FluxComponent connectToStores="user">
<TopNav />
</FluxComponent>
<div>
<div>Left nav</div>
<div>Main content</div>
</div>
</div>
</FluxComponent>
)
}
})
export default Application
---
import Application from './Application'
React.render(<Application />, document.getElementById('app'))
# doesn't work!
# Uncaught Error: fluxMixin: Could not find Flux instance. Ensure that your component has either `this.context.flux` or `this.props.flux`.
let Application = React.createClass({
render() {
return (
<div>
<FluxComponent connectToStores="user">
<TopNav />
</FluxComponent>
<div>
<div>Left nav</div>
<div>Main content</div>
</div>
</div>
)
}
})
export default Application
---
import Application from './Application'
import Dispatcher from './Dispatcher'
let dispatcher = new Dispatcher()
React.render(<FluxComponent flux={dispatcher}><Application /></FluxComponent>, document.getElementById('app'))
# it works!
Any idea why?
from flummox.
Ah, there is a very subtle difference. In React 0.12 and below, context is owner-based, not parent-based. (A component's owner is the one that renders it, whereas the parent is the one directly above it in the component tree.) So the first example will not work because the inner FluxComponent it does not have an owner who provides flux
as context. It's kind of hard to explain, but hopefully that's clear.
In React 0.13, context will be parent-based instead facebook/react#2112
from flummox.
Hmm ok I think I get it.
Long story short, if I use React 0.13 the first example should work...right?
Thanks for the explanation! :)
from flummox.
No problem! Yes, I believe the first example should work in React 0.13.
from flummox.
👍
from flummox.
Ah, there is a very subtle difference. In React 0.12 and below, context is owner-based, not parent-based.
This is incorrect. In 0.13, context is still owner-based, and a warning has been added in 0.13 to notify people of the upcoming switch to parent-based context. See facebook/react#3392 (comment)
from flummox.
Yes you're right.
from flummox.
Related Issues (20)
- how to get a child component input value HOT 1
- Missing getActionsAsObject from Actions HOT 5
- Incorrect params passed to async begin handler HOT 1
- More topic guides needed in doc HOT 1
- Context differs problem
- Do not pass flux prop to components wrapped with `connect` HOT 6
- Use Redux HOT 6
- Explanation in README as to why a switch to Redux should be made HOT 5
- Import flummox/component fails with 4.0.0-rc1 HOT 1
- unable to make flummox work with react-router 1.0.0-beta3 HOT 2
- Actions.js grabs all console logs HOT 4
- Wrong link in why-flux-component-is-better-than-flux-mixin.md
- Considering the numbers HOT 1
- Support for React 0.14 HOT 17
- How to only use custom stateGetter for subset of subscribed stores HOT 1
- Maintain module? HOT 4
- FluxComponent does not work on IE10 HOT 24
- Flummox 3.6 is incompatible with IE8 HOT 3
- TestUtils removed from NPM HOT 2
- Deprecation warning with React 15.5 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flummox.