How to replace colour in an image?

In this blog post I am going to show how to replace color in an image, basically changing the color of an object or part which has a common color in it. In general people do this in their daily life…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to Use React and Typescript in Bit.dev

Bit is a powerful tool for web development. It defines the dependencies of web components and exact them to individual libraries, which is very helpful for refactor and reuse the web components. Here, I present a short tutorial to show how to use React JS / Typescript project in bit.dev step by step.

Part I. Manual set up React/Typescript project

1.1. Create a project folder (I created “bit1”), and start vscode there.

$ npm init

It will ask you several questions, such as project name, version, description etc. You can leave all of them default (I set version to 0.0.1). See below:

After questions, npm will create a package.json file based on your answers, if you want to change some entries, you can directly modify package.json file.

1.3. Install required node packages

$ npm i react react-dom react-scripts

these three are required for React.

$ npm i typescript @types/react @types/react-dom

these three are required for Typescript.

1.4. Add start script in package.json

in scripts section add "start":"react-scripts start" , this is a react project start scripts which is from the package we just installed in step 1.3, we will use it later.

Create 2 folders, public and src, in our project folder,

And 3 files, index.html under public; index.tsx and App.tsx under src.

For index.html, you just need basic frame of a html file (I used default code snippet from vscode) plus one more <div> tag with “id=root”, like this :

For index.tsx:

For App.tsx (using ES6):

Now, we have fundamental staff of React project, let’s run it.

$ npm start

In terminal, we would see some information: 1) npm will detect we are using Typescript in our project (App.tsx) and will create a file of tsconfig.json for us; 2) npm does not detect target browser, it add default one to package.json file.

If everything is OK, terminal will show this:

And a new browser window is popped up with “Hello World” :

The file, tsconfig.json, looks like below, it primarily set the Typescript compilation options, including source folder (./src), target language (es5), libraries, etc.

package.json is modified like this, a section of browser list is added:

Good job so far, our React/Typescript is set, lets go to the second part, setup Bit.dev

Part II. Use Bit to publish single component

2.1. Install Bit:

$ npm install bit-bin -g

This is global installation, you need do this only once. Once it is done, check bit installation by$ bit , you should see this:

type $ which bit , you can find where bit is installed.

2.2. Create Bit account, login and logout

2.3. Initialize Bit workspace

In your project folder, type $ bit init ,

It creates a folder .bit and a file .bitmap . Both are almost empty at this time, and Bit will manage them, we don’t need work on them.

IMPORTANT: React libraries, such as react and react-dom, should be singletons, which means only one instance exists in run time. To ensure this, we need specify these run-time package as peerDependencies.

In package.json we move "react":"^16.x.x" and "react-dom": "^16.x.x from "dependencies" to "peerDependencies" , like this:

2.4. Add Bit local component

First, check local status of our project

$ bit status

Since we haven’t add any file/component, it shows nothing to tag or export.

Let’s add our App.tsx by: $ bit add src/App.tsx

Check status: $ bit status

Great! Our first component app is created.

Since we are using React/Typescript, we need compile source files before we export to Bit.

2.5. Compiling

There are two ways to use Bit components in another project:

$ bit import -c bit.envs/compilers/react-typescript

2.6. Build component

We have compiler, now let’s build our components: $ bit build for all components, or $ bit build app for app (App.tsx) only.

A new folder, dist, is created, with 2 compiled files.

2.7. Publish component

Terminal $ bit status again

$ bit tag --all to lock your changes. Note: the version number is 0.0.1

Go to Bit.dev website, create a new Collection ( I created one named “testing”)

Terminal $ bit login to ensure you are still logged in.

Terminal $ bit export yourUserName.collectionName

Go to Bit.dev website, check our collection of “testing”, newly published app v0.0.1 is there.

Congratulations, your first Bit component is published! But we are not done yet, we need wrap our work.

2.8. Test component at Bit.dev

We already tested our app component locally at step 1.6. Here, we are going to test it at Bit.dev. Click newly published component in last step.

1: index.js sample project entry point file;

2: The code of index.js. It shows how to use component, this could be a demo for other users;

3: Console, if something is wrong when running our component , check information here;

4: Code running result, based on code in 3;

5: Once 4 displays correct result, click save button to save code in 3

6: Preview image of component, which is shown in collection view. Click on it, then check the circle to generate preview. The below is saved preview:

Component with saved preview

The source code of component is also available for others.

2.9. How to use components from Bit.dev

For others who want to use this component, they can do these ways:

$ npm install @bit/yourUserName.collectionName.componentName

$ yarn add @bit/yourUserName.collectionName.componentName

$ bit import yourUserName.collectionName/componentName

Then, import component like this:

Good job, you have read the most useful parts of this article. In next part, I am going to show some extra staff about extracting and publishing multiple components from same project using Bit. If you are interested, keep reading.

Part III. Multiple components

3.1. Add components to project

We are going to modify our project by adding more component. Under src folder, create a new folder components , under it, create a new file Button.tsx ; Button component takes 3 props: isDisabled , onClick and title .

App.tsx is also updated, 3 components, one is <input> , the other two are <Button> , with 3 states (easier to track):

<input> takes text input, first <Button> shows input text at bottom, second <Button> toggles first <Button> active/inactive.

3.2. Add components

Terminal $ bit status

As you see, Bit detected new dependencies. We need add/track Button.tsx

$ bit add src/components/Button.tsx

run $ bit status again

New component button is added.

3.3. Compiling

$ bit build

Both button and app are compiled.

3.4. Publishing components

Double-check status: $ bit status

Lock versions: $ bit tag --all

As you see, the version of app is updated to 0.0.2

Export to Bit.dev, “testing” collection $ bit export vagacoder.testing

Goto Bit.dev website, 2 components are list in “testing”. Note: the version of app is updated to v0.0.2.

3.5. Wrap components

Click on testing/app. Since app component has no props, the default component is same as what we saw in local. Click “New Draft” button below, waiting “loading playground”, click on new “draft”, click “save” on top, back to “draft” button (it becomes “generating preview” now), check circle at corner. We are done with app .

Back to “testing” collection, click on button

Since <Button> takes 3 props, the default example code passes none of them, what we see here is merely an empty button (box 1). We are going to pass props to <Button> (box 2).

Now, <Button> has title, and is disabled. Save and generating preview.

Back to “testing” collection, previews of both components are updated.

As what you have seen in this small project, Bit is able to distinguish different components in same project, draw their dependency map, and extract them as individual ones. This is remarkably helpful for web developers to refactor their products, since building components are natural in frames like React and Vue.

Add a comment

Related posts:

Five reasons why Kintsugi should live on Kusama

In the last couple of days, the Kusama auctions have been fascinating to watch as projects are caught in an intense competition with each other to get a slot. Communities are actively cheering…

Customer Experience

Se dovessi pensare alla capacità di creare prodotti, servizi ma soprattutto esperienze e percorsi emotivi veri e propri per gli utenti, penserei probabilmente in un approccio contrario alla frase…

My List of Top 3 Pro Leagues in Asia

Bago ako maging isang successful businessman sa industriya, pinangarap ko maging isang sportsman. Madalas kong sinusundan ang sports industry lalo na ang basketball. Kabilang ako sa mga lalakeng…