,你寫的是一個無狀態的功能組件,這是應該的工作就像一個正常功能的組件。在這種情況下沒有狀態對象。您需要一個類構造函數才能訪問組件中的狀態對象。
import React from 'react';
import { ListGroup, Alert, Row, Col} from 'react-bootstrap';
import Paper from 'material-ui/Paper';
import TextField from 'material-ui/TextField';
import FlatButton from 'material-ui/FlatButton';
import RaisedButton from 'material-ui/RaisedButton';
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
import DropDownMenu from 'material-ui/DropDownMenu';
class Widget extends React.Component {
constructor(props) {
super(props);
}
handleChange(event, index, value) {
console.log("handle change (value) ", value);
console.log("handle change (event) ", event);
console.log("handle change (index) ", index);
//Set States
}
render() {
const documentList = this.props.documentList;
//ES6 object destructing
// const { documentList } = this.props;
return() {
if(!documentList.length > 0) return;
<div>
<Paper style={{ paddingTop: 16,
paddingBottom: 16,
marginTop: 3,
}}>
<form style={{ padding: 30 }} className="add-update-form" onSubmit={() => false}>
<Row>
<Col md={2}>
<DropDownMenu value={2} onChange={handleChange} openImmediately={true}>
<MenuItem value={1} primaryText="Starter" />
<MenuItem value={2} primaryText="Mains" />
<MenuItem value={3} primaryText="Drinks" />
</DropDownMenu>
</Col>
</Row>
</div>
}
}
}
export default Widget;
這是當前版本Widget.js
import React from 'react';
import { ListGroup, Alert, Row, Col} from 'react-bootstrap';
import Paper from 'material-ui/Paper';
import TextField from 'material-ui/TextField';
import FlatButton from 'material-ui/FlatButton';
import RaisedButton from 'material-ui/RaisedButton';
import Snackbar from 'material-ui/Snackbar';
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
import DropDownMenu from 'material-ui/DropDownMenu'
import Toggle from 'material-ui/Toggle';
class Widget extends React.Component {
constructor(props) {
super(props);
}
handleChange(event, index, value) {
console.log("handle change (value) ", value);
console.log("handle change (event) ", event);
console.log("handle change (index) ", index);
//Set States
}
styles = {
block: {
maxWidth: 250,
},
toggle: {
marginBottom: 16,
},
thumbOff: {
backgroundColor: '#ffcccc',
},
trackOff: {
backgroundColor: '#ff9d9d',
},
thumbSwitched: {
backgroundColor: 'red',
},
trackSwitched: {
backgroundColor: '#ff9d9d',
},
labelStyle: {
color: 'red',
},
customWidth: {
width: 200,
},
};
render() {
const documentList = this.props.documentList;
return() => {
documentList.length > 0 ?
<Paper style={{
paddingTop: 16,
paddingBottom: 16,
marginTop: 3,
}}>
<form style={{padding: 30}} className="add-update-form" onSubmit={() => false}>
<Row>
<Col md={2}>
<DropDownMenu value={2} onChange={this.handleChange} openImmediately={true}>
<MenuItem value={1} primaryText="Starter" />
<MenuItem value={2} primaryText="Mains" />
<MenuItem value={3} primaryText="Drinks" />
</DropDownMenu>
</Col>
<Col md={2}>
<Toggle
label="Dessert"
labelPosition="right"
style={this.styles.toggle}
/>
</Col>
</Row>
<RaisedButton label="Save" primary={true} style={{marginRight: 15, marginTop: 15}} onClick=""/>
</form>
</Paper> :
<Alert bsStyle="warning">No stuff yet.</Alert>
}
}
}
Widget.propTypes = {
documentList: React.PropTypes.array,
};
export default Widget;
我從概念上得出這個結論。雖然此代碼不起作用。即使在添加表單和紙張的缺失結束標記之後。帶空白網站的末端和此錯誤:應該提供一個子組件來構建更高級的容器 –
我希望您對自己的工作有很好的理解。這應該解決你的setState問題。您在容器組件中導入此組件,並且它應該工作得很好,除非您做錯了什麼 –
未捕獲的錯誤:應提供一個用於構建較高階容器的子組件。試圖弄清楚......我確信我正在做一些沒有道理的事情。 –