I have been trying to use create-react-library and so far it works, but I can only import components successfully from index.js. If I try to make another file , I recieve an import error.
The file structure is as such
example
\ Node Module
\ public
\ src
| App.js
| index.js
...
src
\ Patterns
| index.js
| button.js
Currently I can only successfully import components from index.js of the main src. Is there a way to successfully import components from folders such as Patterns or another file?
\ App.js ( example )
Importing button gives me an error "Cant import button from neo"
import React from 'react'
import { ExampleComponent,Button} from 'neo'
import {Test} from 'neo/Patterns';
import 'neo/dist/index.css'
const App = () => {
return (
<>
<Test />
<Button text='Click me' />
<ExampleComponent text="Create React Library Example 😄" />
</>
)
}
export default App
Please check if this is what you're trying to achieve.
index.js will be exporting required components like this,
import ExampleComponent from './ExampleComponent/ExampleComponent';
// ExampleComponent is placed inside a folder named ExampleComponent
import Patterns from './Patterns/Patterns';
// Patterns is placed inside a folder named Patterns
export { ExampleComponent, Patterns };
Patterns.js can look like this,
import React from 'react'
const Patterns = () => {
return <div>Patterns Component sample</div>
}
export default Patterns;
ExampleComponent.js can look like this,
import React from 'react'
import styles from './styles.module.css'
const ExampleComponent = ({ text }) => {
return <div className={styles.test}>Example Component: {text}</div>
}
export default ExampleComponent;
In the consumer level (in this case, example folder), in any jsx, like App.js you can refer those exported components like,
import { ExampleComponent, Patterns } from 'neo'
return (
<Patterns />
)
Related
Let's say I have two React components:
import { React } from "react";
import "./styles.css";
function ComponentA() {
...
}
export default ComponentA;
import { React } from "react";
import "./styles.css";
function ComponentB() {
...
}
export default ComponentB;
Both of these components are importing the same CSS file styles.css. Now let's say that in my app, I'm importing both of these components, so App.js looks something like this:
import { ComponentA } from "./ComponentA";
import { ComponentB } from "./ComponentB";
function App() {
return (
<div className="App">
<ComponentA />
<ComponentB />
</div>
);
}
export default App;
What exactly happens here? Does my app import the same CSS file twice? And if so, do I just make the import on the App.js file instead?
It's as if it was only imported once.
When a module bundler like Webpack sees an import, it puts the following path in the list of files to process, if the path doesn't exist already. The file is only processed once, no matter how many times it's imported.
Note that with React apps, you will often see
import React from "react";
in tens or hundreds of files - React isn't created anew hundreds of times. Rather, the module is processed once, and then files that import it are given access to what React has exported.
Importing CSS files works the same way, in that they're only processed once no matter how many times an import exists for them (though they don't really have exports, just side-effects).
If both ComponentA and ComponentB depend on styles.css, feel free to keep importing styles.css in both - it doesn't hurt, and will make things easier to manage when you can see at a glance that both components directly depend on that CSS.
okay, so i'm new to the react world, and i was learning about the react useContext, i followed exactly what a tutorial on youtube did, i'm trying to build a little project, i tried following his steps and then i hit an error while trying to use the state i literally want it to be accessible accross my app components, inside my app i've created various componenents and also various responsibility that combines all the reusuable components and do a specific task with it. i'm now trying to access my context from my GETCLICKEDIMAGES file, below are my steps
StateContext.jsx file
import React, { useState, createContext } from "react";
export const StateContext = createContext();
export const StateProvider=(props)=> {
//all the components in this app will share this state.
const [names, setNames] = useState([{ name: "Zucci Daniel! its working" }]);
return (
<StateContext.Provider value={"helo"}>{props.children}</StateContext.Provider>
);
}
App.js
import React, { Component } from "react";
import BigWrapper from "./justComponents/BigWrapper/BigWrapper";
import NavBar from "./justComponents/NavBar/NavBar";
import MainContainer from "./justComponents/MainContainer/MainContainer";
//below are the various responsibilities component
import SEARCHANDDISPLAY from "./Responsibilities/SEARCHANDDISPLAY/SEARCHANDDISPLAY";
import { GETCLICKEDIMAGES } from "./Responsibilities/GETCLICKEDIMAGES/GETCLICKEDIMAGES";
import { StateContext } from "./StateContext/StateContext";
class App extends Component {
render() {
return (
<StateContext>
<BigWrapper>
<NavBar />
<MainContainer>
<SEARCHANDDISPLAY />
<GETCLICKEDIMAGES />
</MainContainer>
</BigWrapper>
</StateContext>
);
}
}
export default App;
GETCLICKEDIMAGE.jsx
import React,{useContext} from "react";
import ClickedImageHolderDiv from "../../justComponents/ClickedImageHolderDiv/ClickedImageHolderDiv";
import Figure from "../../justComponents/Figure/Figure";
//you wanna use the stateContext right? import the context here as so;
import { StateContext } from "../../StateContext/StateContext";
//this is responsible for getting the clicked images and displaying them in full details
/**
a DIV to hold the FIGURE
*/
export const GETCLICKEDIMAGES=()=> {
const value =useContext(StateContext);
return (
<ClickedImageHolderDiv>
<h2>{value}</h2>
<Figure useThisStyle={{ height: "100%" }} />
</ClickedImageHolderDiv>
);
}
i don't know if i'm missing something, or something's changed, pls help.
Import StateProvider in your app.js instead of stateContext
I'm creating a custom react library. I'm using typescript, redux, and create-react-library to create it.
It' all ok, I can import my library from other projects, but my problem is that when I do npm run build. This generates a dist folder but doesn't add styles folder with scss and other files from fontello.
I have to create and script that when the post-build copy styles folder into the dist folder.
Also, in index.d.ts file of my library I have this import: import "./styles/main.scss"; but doesn't import it. (Maybe I have to add something in tsconfig.json)
import "bootstrap/dist/css/bootstrap.css";
import "./styles/main.scss";
import React from "react";
import MainTree from "./components/main";
import HierarchyTreeReducerState from "./redux/reducers/HierarchyTreeReducer";
import TreeReducerState from "./redux/reducers/TreeReducer";
interface Props {
portal: boolean;
removeBranchWithChildren: boolean;
data?: any;
}
function TreeLibrary({ portal, removeBranchWithChildren, data }: Props) {
return (
<MainTree
removeBranchWithChildren={removeBranchWithChildren}
data={data}
portal={portal}
/>
);
}
export { TreeLibrary, HierarchyTreeReducerState, TreeReducerState };
The only solution I have found is to import the main.scss from the project that imports the library, like this:
import "react-tree-library/dist/styles/main.scss";
App.tsx from a project that imports my library:
import React from "react";
import { TreeLibrary } from "react-tree-library";
import "react-tree-library/dist/styles/main.scss";
import { completeData } from "./mockedData/huge";
class App extends React.Component<any, any> {
render() {
return (
<TreeLibrary
portal={false}
data={completeData}
removeBranchWithChildren={false}
/>
);
}
}
export default App;
But I would like that when import my library I don't need to import styles manually.
How can I solve this?
Is it possible to limit the import of React components to it's own directory? So it is only possible to import inside the same directory?
For example I have the following dir strutcture:
src/
apps/
myapp.js
/table
table.js
table-row.js
The file table-row.js has the following component
export const TableRow = ({row, index}) => {
return <TableRow> ... </TableRow>
};
How to make TableRow private to /table dir, so I can not import outside this directory? But the table/table.js can import?
You cant actually do this, but a pattern that I like to follow is to do something like
src
components
MyComponent
MyComponent.jsx
PrivateHelperComponent.jsx
index.js
where the components resemble the follow:
//MyComponent.jsx
const MyComponent = () => {
return (
<div>
...
<PrivateHelperComponent />
</div>
)
>
export default MyComponent
//PrivateHelperComponent.jsx
const PrivateHelperComponent = () => {
return (
<div>
...
</div>
)
>
export default PrivateHelperComponent
//index.js
export { default } from "./MyComponent"
...Now the interface for importing from <...>/components/MyComponent is "clean", and you can only import the "default" component.
Of course, nothing stops you from doing import PrivateHelperComponent from "<...>/components/MyComponent/PrivateHelperComponent", but at least in my opinion it clear what components are meant to be imported/reused and what aren't
There is no access modifier for exports in JS (all exports are accessible to all modules; aka public), so you cannot make an export private to its directory.
You can’t do that. But if you really want to make it private put table row (with no export) and table in the same file.
This question already has answers here:
Is it possible to import modules from all files in a directory, using a wildcard?
(14 answers)
Closed 5 years ago.
I currently have my project folder as such
app
- node_modules
- public
- src
- Assets
- Components
- profiles
- homepage
- gulpfile
In my homepage component, I am trying to import the whole directory of profiles into an object like this (dynamically load each component as needed, but not all components will be loaded)
import Comps from '../profiles/';
also
import * as Comps from '../profiles/;
but I get an error in the compiler saying Module not found: Can't resolve '../profile.
I want to make it clear that profiles is a folder with components, and I want to select all components to call dynamically in my app.js file.
Why is this happening?
Do you have several components in one js file, if so - you could do something like this:
Profiles.js:
import React from 'react';
export const ViewA = () => {
return( <div>A VIEW</div>)
};
export const ViewB = () => {
return( <div>B VIEW</div>)
};
export const ViewC = () => {
return( <div>C VIEW</div>)
};
export const ViewD = () => {
return( <div>D VIEW</div>)
};
App.js:
import React, { Component, PropTypes } from 'react'
import * as Profiles from './Profiles';
class App extends Component {
render(){
return (
<div>
<Profiles.ViewA/>
<Profiles.ViewB/>
<Profiles.ViewC/>
</div>
)
}
}
export default App;