1

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>
1
  • can you share a running example? Commented May 6, 2019 at 13:57

1 Answer 1

2

Seems like you have a problem with the asynchronicity of setState(). setState has a second optional parameter, which is a callback function. Just add your dialog into the callback function of setState().

this.setState({filterArray}, () => {this.dialog.current.handleClickOpen()})

UPDATE

parseInput = async () => {
  console.log(this.state.textInput)
  let tempArray = this.state.textInput.split(" "); // `convert string into array`
  let newArray = tempArray.filter(word => word.endsWith("a"));
  let filterArray  = newArray.map(word => word.replace('a', 'b'));
  await this.setState({filteArray: filterArray})
  this.dialog.current.handleClickOpen()
};

Ok, you need to add async and await here. THat should do it.

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried your version and after testing it happened that: 1click: dialog called, state have not changed. 2click: dialog called and state is changed.
do you have any idea why that would happen?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.