0
点赞
收藏
分享

微信扫一扫

React 动态填加class,使用classnames库代码

React 动态填加class,使用classnames库代码_html

使用classnames:

一个简单的JavaScript实用程序,用于有条件地将类名连接在一起。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>React 动态填加class</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
<script src="https://cdn.bootcss.com/classnames/2.2.5/index.js"></script>
<style type="text/css">
.m-test{width: 100px;height: 100px;background-color: red;}
.m-test.active{background-color: blue;}
</style>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {
isToggleOn: true,
active: true
};

// 这个绑定是必要的,使`this`在回调中起作用
this.handleClick = this.handleClick.bind(this);
}

handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn,
active: !prevState.isToggleOn
}));
}

render() {
let testClass = classNames({
"m-test": true,
active: this.state.active
});
return (
<div>
<button onClick={this.handleClick}>
{this.state.isToggleOn ? 'ON' : 'OFF'}
</button>
<div className={testClass}></div>
</div>
);
}
}

ReactDOM.render(
<Toggle />,
document.getElementById('root')
);
</script>
</body>
</html>

 原生写法:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>React 动态填加class</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
<style type="text/css">
.m-test{width: 100px;height: 100px;background-color: red;}
.m-test.active{background-color: blue;}
</style>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {
isToggleOn: true
};

// 这个绑定是必要的,使`this`在回调中起作用
this.handleClick = this.handleClick.bind(this);
}

handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}

render() {
return (
<div>
<button onClick={this.handleClick}>
{this.state.isToggleOn ? 'ON' : 'OFF'}
</button>
<div className={"m-test " + (this.state.isToggleOn ? 'active' : '')}></div>
</div>
);
}
}

ReactDOM.render(
<Toggle />,
document.getElementById('root')
);
</script>
</body>
</html>

ES6写法, 在chrome里调试时会影响js代码,不推荐。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>React 动态填加class</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
<style type="text/css">
.m-test{width: 100px;height: 100px;background-color: red;}
.m-test.active{background-color: blue;}
</style>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {
isToggleOn: true
};

// 这个绑定是必要的,使`this`在回调中起作用
this.handleClick = this.handleClick.bind(this);
}

handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}

render() {
return (
<div>
<button onClick={this.handleClick}>
{this.state.isToggleOn ? 'ON' : 'OFF'}
</button>
<div className={`m-test ${this.state.isToggleOn ? 'active' : ''}`}></div>
</div>
);
}
}

ReactDOM.render(
<Toggle />,
document.getElementById('root')
);
</script>
</body>
</html>


举报

相关推荐

0 条评论