【发布时间】:2019-04-19 06:23:42
【问题描述】:
我的网站有 2 个主题。当我第一次导入一个主题时,它被导入了,第二次它没有改变。我需要更改主题覆盖 css。我该如何解决这个问题?
import React from 'react';
import { Settings } from 'react-feather';
import Modal from 'react-responsive-modal';
export default class ContentConfig extends React.Component {
constructor(props) {
super(props);
this.state = {
openModal: false,
checked: true
};
}
openModal = () => {
this.setState({openModal: true});
}
closeModal = () => {
this.setState({openModal: false});
}
changeRadio = (bool) => {
this.props.state(bool);
this.setState({checked: bool});
}
render() {
return(
<div className="contentConfig">
<div onClick={this.openModal} className="editContentIcon">
<Settings />
<p>Settings</p>
</div>
<Modal open={this.state.openModal} onClose={this.closeModal} center>
<form style={{pading: 20}}>
<legend>Choose a color</legend>
<div className="fieldset">
<div className="colorsBox">
<label htmlFor="radio1" className="colors" style={{background: "#5dafe5"}}></label>
<input
id="radio1"
type="radio"
name="colors"
onChange={() => this.changeRadio(true)}
checked={this.state.checked}
/>
</div>
<div className="colorsBox">
<label htmlFor="radio2" className="colors" style={{background: "#d94f5c"}}></label>
<input
id="radio2"
type="radio"
name="colors"
onChange={() => this.changeRadio(false)}
checked={!this.state.checked}
/>
</div>
</div>
</form>
</Modal>
</div>
);
}
}
这是一个更改主题的子组件,当我选择一个主题时,它会调用父组件中的一个函数。
import React, { Component } from 'react';
import './App.css';
import ContentConfig from './ContentConfig.js';
export default class App extends Component {
constructor() {
super();
}
changeStyles = (bool) => {
if(bool) {
import('./LigthTheme.css').then(() => console.log('imported ligth theme'));
} else {
import('./DarkTheme.css').then(() => console.log('imported dark theme'));
}
}
render() {
return (
<div className="mainBox">
<div className="menu">
<h1>Devices</h1>
<ul className="links">
</ul>
<ContentConfig state={this.changeStyles} />
</div>
</div>
);
}
}
当我第一次在浏览器中更改主题时,它会显示添加到标题的内容。第二次更改主题后,标题中没有任何变化。
【问题讨论】:
-
我看到您没有将任何值传递给 bool 中的 changeStyles 参数。您将需要传递基于布尔值的用户选择,然后导入将更改。如果我错过了什么,请告诉我
-
一开始在 ContentConfig 组件的输入中我设置了
onChange={()=>this.changeRadio (true)}方法。然后我在changeRadio药物中的renderstate={this.changeStyles}方法中向处于渲染状态的父组件this.props.state(bool)发送一个布尔值,并调用changeStyles方法。
标签: javascript reactjs ecmascript-6