-
Notifications
You must be signed in to change notification settings - Fork 293
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
require.context #131
Comments
I love the feature of just doing I'm not in favor of removing it by default. |
Initially I did love the opportunity to import modules from I have a LoginPage container that map some props to the LoginPage (page) component which in turn render (among other things) a If in my LoginPage component I import the LoginForm container like this Sorry fo the headache, I hope the above scenario somehow make sense. |
Node resolves those circular dependencies at runtime. Just remember to wrap the components. These are the rules:
Following those rules should be enough. We should add it to the new Wiki. Please, tell me if there's another use case. |
Thanks @zentuit and @diegohaz for your suggestions; eventually wrapping the component on the container solved the issue. This feels like a work around but not a big compromise to live with so, going back to the original topic, I agree that the benefits are more than the disadvantages hence I vote against removing it by default. I also take the opportunity of this comment to thank you @diegohaz for sharing such a good work, your boilerplate is exactly what I was looking for. |
Not sure how nicely Also, I personally love WebStorm's automatic imports (Alt + Enter or Cmd + Enter automatically adds the correct, most exact, import at the top). This is simply not applicable with I've had issues running this code in SSR using Finally, I've had issues with this when trying to run the server for SSR without bundling. I have a development server that uses |
Maybe we can add a npm script to automatically add files to |
https://github.com/gajus/create-index This might be useful? |
@advance512 Am I correct in assuming it's an automated way of doing this below? Could be useful in making generating the above file automated and easy, but maybe it would create some confusion for new users who just want to understand how to add/import/export new components without having to read the README. |
@declanelcocks yes, of a sorts. The main point is not confusing WebStorm's new feature: auto import of React components. This is also relevant for actions, and in my case for GraphQL queries. |
@advance512 Out of curiosity did you try to implement something like the above? |
No, I actually completely discarded this method. I also stopped exporting as default, rather I export by the identifier (e.g. control, class, whatever) name. It makes auto-imports almost as good as in Python.. and I don't care about the paths as I never manually write them. |
I was thinking about how would look a generic CLI to completely replace our usage of Generate components index (--export-folder)$ generate-index "src/components/*/*" --output "src/components/index.js" --export-folder --watch Output: export Atom from './atoms/Atom'
export Button from './atoms/Button'
... So we can use it as we do today: import { Atom, Button } from 'components' Generate actions index (--export-all)$ generate-index "src/store/*/actions.js" --output "src/store/actions.js" --export-all --watch Output: // is this possible?
export * from './modal/actions'
export * from './post/actions'
... So we can use it as we do today: import { modalShow } from 'store/actions' Generate middleware index (--export-default-array)$ generate-index "src/store/*/middleware.js" --output "src/store/middlewares.js" --export-default-array --watch Output: import entities from './entities/middleware'
...
export default [
entities,
...
] So we can use it as we do today: import middlewares from 'store/middlewares'
const configureStore = initialState =>
createStore(reducer, initialState, applyMiddleware(...middlewares)) Generate reducers index (--export-default-object)$ generate-index "src/store/*/reducer.js" --output "src/store/reducers.js" --export-default-object --watch Output: import entities from './entities/reducer'
import modal from './modal/reducer'
import post from './post/reducer'
...
export default {
entities,
modal,
post,
...
} So we can use: import { combineReducers } from 'redux'
import { routerReducer as routing } from 'react-router-redux'
import reducers from 'store/reducers'
export default combineReducers({
routing,
...reducers,
}) Generate selectors index (--export-all-named)$ generate-index "src/store/*/selectors.js" --output "src/store/selectors.js" --export-all-named --watch Output: export * as entities from './entities/selectors'
export * as modal from './modal/selectors'
export * as post from './post/selectors'
... So we can use: import upperFirst from 'lodash/upperFirst'
import * as modules from 'store/selectors'
export default Object.keys(modules).reduce((rootSelectors, moduleName) => {
const fromName = `from${upperFirst(moduleName)}`
const moduleSelectors = modules[moduleName]
const getState = (state = {}) => state[moduleName] || {}
rootSelectors[fromName] = Object.keys(moduleSelectors)
.filter(key => typeof moduleSelectors[key] === 'function')
.reduce((object, key) => {
const selector = moduleSelectors[key]
object[key] = (state, ...args) => selector(getState(state), ...args)
return object
}, {})
return rootSelectors
}, {}) And import it as we do today: import { fromEntities, fromPost } from 'store/selectors'
fromEntities.getDetail(state, fromPost.getDetail()) |
@diegohaz You're using a library for this |
@declanelcocks No, that's just an interface idea for a possible library. |
Ah ok, makes sense. I suppose some of the code from the current |
@diegohaz Also noticed that even doing |
What I ended up doing was this for my
And for my
This way you can do:
|
How is this working with |
@ghalex |
I'm creating this issue to unify the discussions about
require.context
.While that's very handy to import components, it comes with some drawbacks like #130, #113 and #128 (comment)
Personally, I don't have problems with it, the advantages are bigger than the issues for me. But, as many are having problems, we should consider removing it by default or at least give proper instructions on how to remove it.
If you are using this webpack feature (whether your project is based on ARc or not), please share your thoughts.
The text was updated successfully, but these errors were encountered: