React.js ライブラリ「react-fast-marquee」を使ってmarqueeを作成する

React.js ライブラリ「react-fast-marquee」を使ってmarqueeを作成する

ライブラリ「react-fast-marquee」をインストールすると、marqueeを作成することが可能です。ここでは、react.jsでreact-fast-marqueeを利用するための手順と簡単な使い方を記述してます。

環境

  • OS  CentOS Stream release 8
  • node V14.15.1
  • npm 6.14.8
  • React 16.13.0

react.js環境構築

下記のコマンドで構築してます。ここでは、react-appという名前でプロジェクトを作成してます。

create-react-app react-app

react-fast-marqueeインストール

作成したプロジェクトに移動して、インストールします。

## 作成したプロジェクトに移動
cd react-app
 
## インストール
npm i react-fast-marquee

react-fast-marquee使い方

srcディレクトリにsample.jsと名前で下記のコードを記述します。

import React from "react"
import Marquee from "react-fast-marquee"

const Sample = () => {  
  const style = {    
    margin: "0 auto",
    marginRight: 30,
  };
  return (
    <div>
      <Marquee>
        <div style={style}>text1 text1</div>
        <div style={style}>text2 text2</div>
        <div style={style}>text3 text3</div>
      </Marquee>
    </div>
  );
};

export default Sample;

次に、srcディレクトリ配下にあるApp.jsを下記のように編集します。

import React from 'react';
import Sample from './sample';
import './App.css';

function App() {
  const style = {
    width: "50%",
    margin: "0 auto",
    marginTop: 150,
  };
  return (
    <div className="App">
      <div style={style}>
        <Sample />
      </div>
    </div>
  );
}

export default App;

実行します。

npm start

ブラウザから http://プライベートIP:3000にアクセスすると、marqueeが作成されていることが確認できます。