javascript - 一段时间后如何更改组件的类别?
问题描述
我正在使用渲染 SVG 图像但不渲染填充的组件。我做了两节课
.svg {
fill-opacity: 0;
.svg-show {
fill-opacity: 1;
transition: fill-opacity 2s;
}
我的问题是在假设持续时间为 200 之后如何将类名 svg-show 应用于以下组件
<ReactVivus
className="svg"
option={{
file: forever,
duration: 200,
animTimingFunction: "oneByOne",
type: "delayed",
onReady: console.log
}}
callback={console.log}
/>
解决方案
在组件中有一个状态变量,然后根据该变量选择类名。在类组件中,它看起来像这样:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
show: false,
}
}
componentDidMount() {
setTimeout(() => this.setState({ show: true}), 200);
}
render() {
return (
<ReactVivus
className={this.state.show ? "svg-show" : "svg"}
option={{
file: forever,
duration: 200,
animTimingFunction: "oneByOne",
type: "delayed",
onReady: console.log
}}
callback={console.log}
/>
)
}
}
推荐阅读
- postgresql - postgresql 查询等效于弹性搜索中具有扩展边界的日期直方图
- java - 从集合列表中提取相似集合的解决方案
- python - Socket.io 客户端不会显示给所有用户
- sql - 从会话日志中计算并发会话数 (SQL / BigQuery)
- oauth-2.0 - 如何在 Stata 中的 Box 上读取 CSV 文件
- java - eclipse中是否应该将WebContent文件夹添加到BuildPath?
- javascript - 如何验证 React 组件中的条件表单字段?
- reactjs - 我应该将数据传递给组件还是向 React 中的 API 发出请求?
- sql - Should I always use dynamic sql when programatically use some stored procedure?
- angular - 在处理编辑/更新的反应形式时出现错误“打字稿中的对象可能为'null'”