Click "Use this template" to generate a
new repository.
Then open your terminal and clone your repository:
cd ~/Desktop
git clone https://github.com/[your-user-name]/[your-repo-name].git
Go to the root of your repository's folder, and install all dependecies:
cd ~/Desktop/[your-repo-name]
npm install
Create your components and/or logic, then when you'd like to test your code, create a new React app, and link the package to it:
cd ~/Desktop
npx create-react-app [test-app-name]
cd [your-repo-name]
npm link
cd [test-app-name]
npm link [your-repo-name]
Now you can import your components in the testing application as if it were installed from NPM.
Before building and publishing, there are a few paramters that you need to tweak:
- In
package.jsonupdate thenamefield, this is the name that will be used on NPM (unique). - In
package.jsonmake sure theprivatefield isfalse. - In
package.jsonyou can make other optional changes, but do NOT change thepeerDependencies,main, andtypesfields.
Now to build and publish run the following commands:
cd [your-repo-name]
npm run build
npm login
npm publish
The top part of of this readme is for you as a developer, the bottom part (from this parapagraph) is for the user, tweak it according to your library, and then you should publish the package.
Install from npm:
npm i --save <package name>Then use it in your app:
import Example from '<package name>'
function App(){
const handleClick = () => {
console.log('Clicked!')
}
return (
<Example onClick={handleClick} >
Just an example...
</Example>
)
}Common props you may want to include:
| Prop | Requirement | Default Value | Description |
|---|---|---|---|
| children | Optional | 'Button' | Should contain the button's text |
| style | Optional | {} | React.CSSProperties style object |
| className | Optional | '' | Additional class name(s) |
| onClick | Optional | () => null | Function triggered by onClick event |
