Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

reactjs - Wait a dispatch finish to get a state

When I open the screen, I want to dispatch an 'edit' action that looks for the item by its id and changes a state property with this information found, then when it get finished, I want to open the screen with this information. I've tried to dispatch on the Constructor and set the state from ComponentDidMount, but it only appears when I reload component. Another thing that I noticed is that when I put an alert on the rendering method I could see the array, but when I send it to the 'EditableList' component it's not shown. Do I need to do an async dispatch?

class Observation extends Component {
constructor(props) {
    super(props);
    this.state = {
        obs: [],
        get: false
    }
    const { edit, route} = this.props;
    edit(route.params.item.id);
}
onChange(list) {
    this.setState({obs: list})
}
componentDidMount() {
    this.setState({obs: this.props.Obs})
}
render() {
    const {route, add, Obs, loading} = this.props;
    return (
        <View style={{flex: 1, paddingTop: StatusBar.currentHeight + 10, backgroundColor: '#FFD618'}}>
            <Text style={{fontSize: 36, padding: 20}}>Observa??es</Text>
            <EditableList style={{ width: '100%'}} list={this.state.obs} onListChange={this.onChange.bind(this)}/>
            <TouchableOpacity style={styles.button} onPress={() => add({...route.params.item, obs: this.state.obs})}>
                <View>
                    <Text style={{color: 'white'}}>Adicionar</Text>
                </View>
            </TouchableOpacity>
        </View>
    );
}
}
const mapStateToProps = state => ({
    Obs: state.cart.Obs
});
const mapDispatchToProps = dispatch => ({
    add: item => dispatch(Add(item)),
    edit: e => dispatch(edit_obs(e))
})
export default connect(mapStateToProps, mapDispatchToProps)(Observation);
question from:https://stackoverflow.com/questions/65866562/wait-a-dispatch-finish-to-get-a-state

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...