The problem is that the state filterArray changes from null only on second click on button "Обучить". Why?
I really need it to work from the first click. Problem is not in dialog that i call. The function parse input is parsing input to catch words with * at the end.
Dialog is called from the first click, but the state is changing only from second.
class Content extends React.Component {
constructor(props) {
super(props);
// initial state
this.state = {
textInput: "",
filterArray: [],
projectID: "",
entity: "",
synonyms: ""
};
this.dialog = React.createRef()
}
clear = () => {
// return the state to initial
this.setState({
textInput: "",
filterArray: []
});
};
updateIdandEntity = (value1, value2) => {
this.setState({ projectID: value1, entity: value2 })
}
parseInput = () => {
let tempArray = this.state.textInput.split(" "); // `convert string into array`
let newArray = tempArray.filter(word => word.endsWith("*"));
let filterArray = newArray.map(word => word.replace('*', ''));
this.setState({filterArray});
this.dialog.current.handleClickOpen()
};
render() {
return (
<Paper style={{maxWidth: 936, marginLeft: "250px", overflow: "hidden"}}>
<AppBar position="static" color="default" elevation={0}>
<Toolbar>
<Grid container spacing={16} alignItems="center">
<Grid item xs>
<TextField
fullWidth
placeholder="Введите фразу которую нужно обучить"
id="textInput"
InputProps={{
disableUnderline: true
}}
value={this.state.textInput}
onChange={e => {
this.setState({textInput: e.target.value});
}}
/>
</Grid>
<Grid item>
<DialogProject ref={this.dialog} data={this.state.filterArray} updateIdandEntity={this.updateIdandEntity}/>
<Button
variant="contained"
color="primary"
style={{
background:
"linear-gradient(45deg, #00ACD3 30%, #00BE68 90%)"
}}
onClick = {this.parseInput}
>
Обучить
</Button>