【发布时间】:2016-08-03 08:27:00
【问题描述】:
假设以下和所有组件/fus/fci/ssg 只有一个带有站点道具的 h1。我想了解为什么它是一个有效的反应元素,但这些元素并没有显示同样的渲染。也就是说,一个有 h1 元素,另一个没有。这个想法是不要为不同的站点创建带有切换的大型组件,并且每个站点都将根据导航选择进行交换。除非我错过了,否则我没有看到任何记录...
{this.state.renderSite}
<Fci site="Fci"/>
import React from 'react';
import styles from './App.css';
import Nav from '../components/Nav.js'
import Fus from '../components/Fus.js'
import Fci from '../components/Fci.js'
import Ssg from '../components/Ssg.js'
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {renderSite: '', site: 'default' };
this.pickSite = this.pickSite.bind(this);
}
pickSite(site){
this.setState({renderSite: React.createElement(site, {"site":site})});
this.setState({site: site});
console.log( React.isValidElement(this.state.renderSite));
}
render() {
return (
<div className={styles.app}>
<Nav site={this.pickSite.bind(this)} />
{this.state.renderSite}
<Fci site="Fci"/>
</div>
);
}
}
导航
import React from 'react';
export default class Nav extends React.Component {
constructor(props) {
super(props);
this.update = this.update.bind(this);
}
update(e) {
this.props.site(e.target.dataset.site);
}
render(){
return (
<div>
<button onClick={this.update} data-site="Ssg"> SSG </button>
<button onClick={this.update} data-site="Fci"> FCI </button>
<button onClick={this.update} data-site="Fus"> FUS </button>
</div>
);
}
}
【问题讨论】:
-
你能发布
Nav组件吗? -
@Aaron 添加了导航组件
标签: dynamic reactjs components