28
loading...
This website collects cookies to deliver better user experience
npx create-react-app
.npm i --D react-cosmos
yarn add --dev react-cosmos
package.json
file in your React project.package.json
script in your application. This will enable you to start your Cosmos project. Include the following code under the script section of your package.json
file:"scripts": {
"cosmos": "cosmos",
"cosmos:export": "cosmos-export"
}
package.json
file should look as so:{
"name": "reactcosmos",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"cosmos": "cosmos",
"cosmos:export" : "cosmos-export"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"react-cosmos": "^5.6.2"
}
}
npm run cosmos
yarn cosmos
localhost:5000
to view your component libraries. You won’t see any components there yet because we haven‘t built any.babel.config.js
and update its content with the code below:
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
['@babel/preset-react', {targets: {node: 'current'}}] // add this
]
};
postcss.config.js
and update its content as so:
module.exports = {}
.fixture
to the name of the file containing our component — we’ll use main.fixture.js
. This way, React Cosmos can easily track it.button.fixture.jsx
within the src
folder of your React application. The next step is to update the file with the code below:// button.fixture.jsx
import React from 'react';
export default function Hello() {
return <h1>Hello, World</h1>
}
localhost:5000
again, where you should see your first component listed under All Fixtures.button.fixture.jsx
file as shown below.import React from 'react';
import './button.fixture.css'
export default function Button() {
return <button> Hey, Click me</button>;
}
button.fixture.css
for our button component styling. Update the content of the new file as follows:button{
padding:10px 20px;
color:#fff;
border:none;
outline:none;
background-color: teal;
border-radius: 8px;
display:block;
margin:auto;
margin-top:40px;
cursor:pointer;
}
localhost:5000
to see the changes we made to our component.src
folder of your React application. You can create a fixture either by adding .fixture
to your filename or creating a __fixtures__
folder and placing all your fixture files within it.object
, as shown below; this will enable you to export multiple components with one fixture file.export default {
primary: <PrimaryButton>Click me</PrimaryButton>,
primaryDisabled: <PrimaryButton disabled>Click me</PrimaryButton>,
secondary: <SecondaryButton>Click me</SecondaryButton>,
secondaryDisabled: <SecondaryButton disabled>Click me</SecondaryButton>,
};