Export default react-native component - reactjs

For my solution I want users to get all components from my library.
Example: import Checkbox from 'MyLibrary';
But for native, I'm don't want to make all the components my self. At least, not now. And that is why I want them to reference my library, so when I do change/write the component, it will automaticly update at their end.
For example, a checkbox. There is a default react-native checkbox (https://facebook.github.io/react-native/docs/checkbox)
I would like to export this default component within my component.
I tried things like:
export { Checkbox as default } from 'react-native';
and
import { Checkbox } from 'react-native';
export default Checkbox;
and
import React from 'react';
import { Checkbox as ReactCheckbox } from 'react-native';
const Checkbox = (...props) => <ReactCheckbox {...props} />;
export default Checkbox;
But that didn't work. Any suggestions?
Edit: I made a mistake with the import, it's CheckBox not Checkbox...

It's CheckBox, not Checkbox, doh!

I'm not quite sure but, while I tried your code it didn't worked on mine too. But, you can do like this:
import { TextInput as myTextInput } from 'react-native';
But, while I checked this on expo app:
import { CheckBox as myCheckBox } from 'react-native';
From this what I think is react native doesn't support CheckBox on Expo app. But, if you libraries like React Native Elements or Native Base. Then you can import checkbox like this.
import { CheckBox as myCheckBox } from 'react-native-elements';
Maybe this Snack will help you.
Happy coding :)

If I understand you correctly, you want to RE-EXPORT the React/Community checkbox as your component and replace it with your own component in future?
Native Checkbox component is only available in Android, so you have to use community built one from UI-Kitten/Native Base/React Native Element.
First explore and pick your UI Library and install it. Then you can import the checkbox component from the respective library and export it until you write your own component.
This will work
import { CheckBox } from 'react-native-elements';
export default CheckBox;
In case you want to wrap the component with default properties or styling then you can do something like below
import { CheckBox } from 'react-native-elements';
export default function(props) {
const yourProps = {
checkedIcon: <Image source={require('../checked.png')} />,
uncheckedIcon: <Image source={require('../unchecked.png')} />
};
return (
<CheckBox {...yourProps} {...props} />
);
}

Related

Is the order of Import statments inside a react component importanr?

Problem:
I am using styled-components to style a react component.
When I use the component inside other components, SOMETIMES I find that the styling either dons't work at all, or I have to reload the page before the styles from the styled-components js file are applied. - The page dosn't show any errors.
The CSS from styled-components just works in some places and doesn't in others, even though it's exactly the same component.
Solution:
After hours of trying to work out why this was the case, the solution was to change the order of the import statements inside the parent component here is an example:
I have a component called "BreadCrumbHeader" and I am using it inside my menu component like this:
import React, { useContext } from 'react';
import { MenuContext } from '../context/menuContext';
import RenderCmsComponents from '../../components/RenderCmsComponents/';
import {MenuPageLink, MenuSection, MenuItemCard, MenuItemCardImg, MenuContainer, MenuRow, MenuItemCardBody, MenuItemTitle} from './menuStyle';
import BreadCrumbHeader from '../../components/BreadCrumbHeader/';
function Menu() {
const [categoryItems] = useContext(MenuContext);
return (
<div>
<BreadCrumbHeader />
<MenuSection backgroundurl="/images/home-slide-4-1920x800.jpg" fluid>
<MenuContainer>
<MenuRow>
{categoryItems.map(categoryItem => (
<MenuItemCard key={categoryItem.Id}>
<MenuPageLink href={categoryItem.CategoryUrl}>
<MenuItemCardImg src={categoryItem.ImageUrl} />
<MenuItemCardBody>
<MenuItemTitle>{categoryItem.CategoryName}</MenuItemTitle>
</MenuItemCardBody>
</MenuPageLink>
</MenuItemCard >
))}
</MenuRow>
</MenuContainer>
</MenuSection>
<RenderCmsComponents />
</div>
);
}
export default Menu;
The styles for the BreadCrumbHeader component do not work when the menu compoent is rendered. HOW EVER If I change the order of the import statments to this:
import React, { useContext } from 'react';
import { MenuContext } from '../context/menuContext';
import RenderCmsComponents from '../../components/RenderCmsComponents/';
import BreadCrumbHeader from '../../components/BreadCrumbHeader/';
import { MenuPageLink, MenuSection, MenuItemCard, MenuItemCardImg, MenuContainer, MenuRow, MenuItemCardBody, MenuItemTitle } from './menuStyle';
//Rest of the code is the same
Then the styles for BreadCrumbHeader are rendered correctly!
It seems like you have to import the styles for the parent component (menu in this case) AFTER you import any child component.
My Question:
I am fairly new to using react and styled components.
What I want to know is how do you know what order the import statments should be in?
Is there a rule you should always follow?
From all the tutorials I have read on styled components I have never known the order of the import statements to be something you need to be careful about unless you are importing CSS files.

Is there a Ionicons: Icon props?

So I am writing a functional component right, I written a props.
So I wanted it to able to auto suggest name attributes. Something like this
import { Ionicons } from "#expo/vector-icons";
export interface ownWrittenProps {
leftIcon?: ????????;
}
so when people start typing <FunctionalComponent leftIcon=" it can suggest the acceptable icon names from Ionicons the reason I want to use it because it pack into the ios/apk package anyway. What should I put in ???????? so that it can suggest the name?
Another question, if I have a textbox in my ChildComponent how do I retrieve the text/value from parent?
#expo-vector-icons exposes a glyphMap for each icon type. You can use this to generate the name suggestions.
import { Ionicons } from '#expo/vector-icons';
export interface ownWrittenProps {
leftIcon?: keyof typeof Ionicons.glyphMap;
}
This will create a type from the map, then make sure that only keys of that type can be used when setting leftIcon.
If suggestions didn't work you can directly find icon name from Ionicons official website and use it like a component.
Example:
import { Ionicons } from '#expo/vector-icons';
<Ionicons name="add-outline" size={24} color="black" />
or
import { Ionicons } from "#expo/vector-icons";
export interface ownWrittenProps {
leftIcon?: "add-outline";
}

find out where one react component is used?

is there a way to find out where a react component is used in other components?
for example:
navbar components is used in app.js
import React from 'react';
const Navbar = () => {
return <div>this is Navbar</div>;
};
export default Navbar;
import React from 'react'; import Navbar from './Navbar';
const App = () => {return (<div><Navbar /></div>); };
export default App;
There's two ways to find:
React Developer Tools extension (recommended) tool for debugging. It has a Search bar (text or/regex/)
Most of IDE/Code editors now days has the ability to help you find out component usage
Cheers and I hope this helps
Most IDEs should be able to find references to the component.
Otherwise, a simple search should find it.

Material-ui withTheme() giving error "TypeError: Object(...) is not a function"

EDIT: I installed the v1.0.0.36 beta version and copied the sample from that versions docs (which looks identical to me) and it worked straight away. Not sure what the problem was but thanks for replies
I am trying to use Material-UI's withTheme to access the theme in a component.
I have followed the sample in the docs which goes through the create-react-app packager ok but in the browser gives the error: TypeError: Object(...) is not a function
and highlights the code line > 17 | export default withTheme()(WithTheme);
I have cut down the sample code to the most basic use of withTheme() and am still receiving this error
withtheme.js
import React from 'react';
import { withTheme } from 'material-ui/styles';
function WithTheme() {
const styles = {
primaryText: {
color: 'red',
}
};
return (
<h1 style={styles.primaryText}>Hello</h1>
);
}
export default withTheme()(WithTheme);
EDIT: To help clarify the problem, here is the App.js file.
import React, { Component } from 'react';
import './App.css';
import 'typeface-roboto';
import AppBar from 'material-ui/AppBar';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import {brown500, brown900} from 'material-ui/styles/colors';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import WithTheme from './components/withtheme';
const Theme = getMuiTheme({
palette: {
primary1Color: brown900,
primary2Color: brown500,
}
});
class App extends Component {
render() {
return (
<MuiThemeProvider muiTheme={Theme} >
<AppBar
title="Title"
iconClassNameRight="muidocs-icon-navigation-expand-more" />
<WithTheme />
</MuiThemeProvider>
);
}
}
export default App;
I have customised the theme and changed primary1Color to brown, using muiThemeProvider. This all works fine when I remove the WithTheme component from App.js - the AppBar is brown as expected. The problem is I am getting the error when I try to use the mui withTheme function.
My intention is to set the h2 in WithTheme component to be whatever color the current theme has for primary1Color
**End Edit
Any help would be appreciated. Happy to post the (almost) exact copy of the doco sample code which achieves the same error if required.
Thanks
As MaterialUI is no longer in Beta, the spec has changed a bit, outdating Liam's answer. Per the Material-UI
v3.1.2 docs
import { withTheme } from '#material-ui/core/styles';
export default withTheme()(WithTheme);
As of Material 4, the spec was changed a bit again: https://material-ui.com/styles/api/#withtheme-component-component (thus outdating Awnage's answer too).
So now it is:
import { withTheme } from '#material-ui/styles';
export default withTheme(MyComponent);
No need to use withStyles() unless if you want to make a specific style for the component
Warp your App with MuiThemeProvider then you are able to use the theme properly
Material-Ui 0.20.0
For access theme colors use getMuiTheme
import getMuiTheme from 'material-ui/styles/getMuiTheme';
export default muiThemeable()(App)
http://www.material-ui.com/#/components/app-bar
Working Demo
Material-Ui 1.0.0 beta
For access theme colors use withTheme
import { withTheme } from 'material-ui/styles';
export default withTheme()(App)
https://material-ui-next.com/demos/app-bar/
Working Demo V1
If you intend to change the theme of material-ui, I would prefer using getMuiTheme. Refer http://www.material-ui.com/#/customization/themes for documentation. What happens here is, you are styling your component with JavaScript and hence the export requires withStyles to be called.
export default withStyles(styles)(WithTheme);

Combining components to one library

Im currently creating a UI library for my react native project.
The components are separated into its own files like: Button, TextBox, Panel etc etc
So when i want to use them I do:
import Button from '../UI/button';
import TextBox from '../UI/textBox';
But how could I impletement the following call instead? Not needing to do import statetments for each specific component.
import { Button,TextBox, SomeOtherComp } from '../UI/??';
This would save a lot of typing when I want to use multiple components...
Create a file called index.js
The purpose of this file is to simple expose all available components from your library
import Button from './button';
import TextBox from './textBox';
...
module.exports = {
Button,
TextBox,
...
};
In the code that consumes your UI library, you can now import the components like this:
import { Button, TextBox, SomeOtherComp } from '../UI';
When you import a folder name, the packages will look for an index.js file and import that.
It is possible to re-export modules directly without writing duplicate code for importing and exporting:
// UI.js
export { default as Button } from './button';
export { default as TextBox } from './textBox';
Usage:
import { Button, TextBox } from './UI';

Resources