React.js ライブラリ「react-sidebar」を使用してサイドバーを作成する
- 作成日 2020.03.21
- 更新日 2020.07.22
- React
- react-sidebar, React.js, ライブラリ

ライブラリ「react-sidebar」をインストールすると、フェードイン等のアニメーションの実装が簡単に可能です。ここでは、react.jsでreact-sidebarを利用するための手順と簡単な使い方を記述してます。
環境
- OS CentOS Linux release 8.0.1905 (Core)
- node V12.13.1
- npm 6.14.2
- React 16.13.0
react.js環境構築
下記のコマンドで構築してます。ここでは、react-appという名前でプロジェクトを作成してます。
create-react-app react-app
react-sidebarインストール
作成したプロジェクトに移動して、インストールします。
## 作成したプロジェクトに移動
cd react-app
## インストール
npm install react-sidebar
react-sidebar使い方
srcディレクトリにsample.jsと名前で下記のコードを記述します。
import React from 'react';
import Sidebar from 'react-sidebar';
class Sample extends React.Component {
constructor(props) {
super(props);
this.state = {
sidebarOpen: true
};
this.onSetSidebarOpen = this.onSetSidebarOpen.bind(this);
}
onSetSidebarOpen(open) {
this.setState({ sidebarOpen: open });
}
render() {
return (
<Sidebar
sidebar={<div><b>サイドバー</b><br></br><b>サイドバー</b></div>}
open={this.state.sidebarOpen}
onSetOpen={this.onSetSidebarOpen}
styles={{ sidebar: { background: "white" } }}
>
<button onClick={() => this.onSetSidebarOpen(true)}>
サイドーバーを開く
</button>
</Sidebar>
);
}
}
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にアクセスすると、サイドバーが実装されていることが確認できます。

-
前の記事
React.js ライブラリ「victory」を使用して棒グラフや線グラフを実装する 2020.03.21
-
次の記事
CentOs7 yum updateができない場合の対処法 2020.03.21
コメントを書く