【发布时间】:2017-12-02 18:22:47
【问题描述】:
我正在尝试使用 reactjs 开发一个 webapp,但我遇到了一个问题。经过1天多的研究,我不明白该怎么做。
我想使用一个组件,它是我页面的主要布局,添加其他组件以在其中显示。
在组件Base2中,子props包含另一个组件。
import React from 'react';
import PropTypes from 'prop-types';
import { Link, NavLink } from 'react-router-dom';
const Base2 = (child) => (
<div>
<div className="top-bar">
<div className="top-bar-left">
<NavLink to="/">React App</NavLink>
</div>
<div className="top-bar-right">
<Link to="/login">Log in</Link>
</div>
</div>
<child/> // HERE the dynamic component
</div>
);
export default Base2;
调用它的函数是:
const TestBase = ({props}) => {
return (<Base child={MyComponent}/>)
};
此外,MyComponent 可以是一个类声明以下两种方法:
import React from 'react';
import LoginForm from '../components/LoginForm.jsx';
class MyComponent extends React.Component{
constructor(props) {
super(props);
...
}
render() {
return (
<LoginForm
onSubmit={this.processForm}
onChange={this.changeUser}
errors={this.state.errors}
user={this.state.user}
/>
);
}
}
export default LoginPage;
第二种方法:
import React from 'react';
import { Card, CardTitle } from 'material-ui/Card';
const MyComponent = {
render() {
return (<Card className="container">
<CardTitle title="React Application" subtitle="Home page." />
</Card>);
}
};
export default MyComponent ;
在我的测试中,只有第二种方法有效。第二种方法缺少“实例”(我猜是这样的)可能是问题所在? 如何开发 Base2 组件来接受这两种组件声明?
提前感谢您的帮助
【问题讨论】:
标签: javascript reactjs components render