如果每個組件都有自己的狀態,那麼這件事情是怎麼發生的, 何其可能是孩子改變父母的狀態?
這裏是我的應用程序的完整代碼:
import React, { Component } from 'react';
import { render } from 'react-dom';
import AutoComplete from 'material-ui/AutoComplete';
import Chip from 'material-ui/Chip';
import Hello from './Hello';
import './style.css';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
class App extends Component {
constructor() {
super();
const vehicles = [{value : 1 , label : 'Vehicle 1'},{value : 2 , label : 'Vehicle 2'},{value : 3 , label : 'Vehicle 3'},{value : 4 , label : 'Vehicle 4'},{value : 5 , label : 'Vehicle 5'},{value : 6 , label : 'Vehicle 6'},{value : 7 , label : 'Vehicle 7'},{value : 8 , label : 'Vehicle 8'}];
this.state = {
vehicles,
name: 'React',
name1: 'React1',
name2: 'React2'
};
}
render() {
const dataSourceConfig = {
text: 'label',
value: 'value',
};
return (
<div>
<MuiThemeProvider>
<div>
<AutoCompleteHlpr
dataSource={this.state.vehicles}
dataSourceConfig={dataSourceConfig}
floatingLabelText='Select Vehicles'
selectedOption={this.handleSelectedVehicle}/>
<AutoCompleteHlpr
dataSource={this.state.vehicles}
dataSourceConfig={dataSourceConfig}
floatingLabelText='Select Vehicles'
selectedOption={this.handleSelectedVehicle}/>
<AutoCompleteHlpr
dataSource={this.state.vehicles}
dataSourceConfig={dataSourceConfig}
floatingLabelText='Select Vehicles'
selectedOption={this.handleSelectedVehicle}/>
</div>
</MuiThemeProvider>
<Hello name={this.state.name} />
<p>
Start editing to see some magic happen :)
</p>
</div>
);
}
}
export class AutoCompleteHlpr extends React.Component {
constructor(props) {
super(props);
this.state = {dataSource : this.props.dataSource , searchText : ''};
this.styles = {
chip: {
margin: 4,
},
wrapper: {
display: 'flex',
flexWrap: 'wrap',
},
};
this.handleNewRequest = this.handleNewRequest.bind(this);
this.getDataSource = this.getDataSource.bind(this);
console.log(this.state);
}
handleNewRequest(searchText , index){
this.state.dataSource[index]['selected'] = true;
this.setState({dataSource : this.state.dataSource , searchText : '' });
this.props.selectedOption(this.state.dataSource[index] , this.state.dataSource);
}
renderChip(data , index) {
if(data.selected) {
var value = this.props.dataSourceConfig.text ? this.props.dataSourceConfig.text : 'text';
var key = this.props.dataSourceConfig.value ? this.props.dataSourceConfig.value : 'value';
return (
<Chip
key={data[key]}
style={this.styles.chip}
onRequestDelete={() => this.handleRequestDelete(index)} >
{data[value]}
</Chip>
);
}
}
handleRequestDelete(index) {
this.state.dataSource[index]['selected'] = false;
this.setState({dataSource : this.state.dataSource , searchText : '' });
this.props.selectedOption(this.state.dataSource[index] , this.state.dataSource);
}
componentWillReceiveProps(nextProps) {
if(this.props.dataSource !== nextProps.dataSource) {
this.setState({ dataSource : nextProps.dataSource });
}
}
getDataSource() {
return this.state.dataSource.map(data => {
if(!data.selected) {
return data
}
})
}
render() {
return(
<div>
<AutoComplete
floatingLabelText={this.props.floatingLabelText}
filter={AutoComplete.caseInsensitiveFilter}
onNewRequest={this.handleNewRequest}
searchText={this.state.searchText}
dataSource={this.getDataSource()}
dataSourceConfig={this.props.dataSourceConfig}
openOnFocus={true}
/>
<div style={this.styles.wrapper}>
{this.state.dataSource.map(this.renderChip, this)}
</div>
</div>
)
}
}
AutoCompleteHlpr.defaultProps = {
floatingLabelText : 'Type Something',
dataSource : [],
dataSourceConfig : {},
selectedOption:() => { }
};
render(<App />, document.getElementById('root'));
這裏是鏈接到stackblitz(在線工作代碼):
https://stackblitz.com/edit/react-svqwcg
如何產生的問題:
- 選擇 「車輛1」 從第三個文本框 第二個文本框
- 選擇 「車輛3」 第一個文本框
- 選擇 「汽車2」
你會明白的。
請在問題本身中包含您的代碼。我不知道這個stackblitz網站是什麼,我不想訪問它,大多數訪問這個問題的人也不會。 –
@JamesDonnelly,請訪問它的一種plunkr,在那個網站上我創建了整個演示的問題,在那裏你可以看到整個項目代碼。 –
問題是,如果該網站出現故障或因其他原因無法訪問,則此問題變得毫無意義。 –