0

I have a problem in my application developed with react native and sql server .

In page X, I want to search by year and month and display the results on the same page of the form, For now I can display them only in an alert

This is my code :

fetch('http://192.168.1.12:80/fetch.php',{
   method:'post',
   header:{
   'Accept':'application/json',
   'Content-type' :'application/json'
   },
   body:JSON.stringify({email:usermail,password:password,})})
   .then((responseData)=>responseData.text()).then((responseJson)=>{
     alert(responseJson)})
   .catch((error)=>{console.error(error);})}
    render() {
        return (
            <View style={styles.container}>
            <View style={styles.pass}>
            <Text style={{marginTop:20 , color:'grey'}}>Annee:</Text>
            <TextInput placeholder={this.state.placeholder1} style={{flex:1,paddingLeft:5}}
            onChangeText= {usermail=>this.setState({usermail })}/>
             </View>
            <View style={styles.pass}>
        <Text style={{marginTop:20 , color:'grey'}}>Mois :</Text>
        <TextInput placeholder={this.state.placeholder2} style={{flex:1,paddingLeft:5}} secureTextEntry={this.state.secureTextEntry}
        onChangeText= {password=>this.setState({password})}
        />
            </View>
            <TouchableOpacity style={styles.butt} onPress={this.userRegister}>
                <Text>Afficher</Text>
            </TouchableOpacity>
            </View>
        );

The form that helps me to filter my data by year and month

the result of my query in alert

James Z
  • 12,209
  • 10
  • 24
  • 44
imanealami
  • 189
  • 5
  • 13

1 Answers1

0

A few things...

First, it might be useful to separate out this request from your component just for ease of debugging in the future! Can I suggest a separate service that handles your data access and returns a list of objects you construct from the returned JSON?

Second, you get responseData back and...alert on it. But you don't store that information off in a variable, or call render from inside that scope. The lambda function you're using knows what responseData means because it's seeing it passed in (then((responseJson)=>{ alert(responseJson) })), but how can render() know about this data if it isn't stored anywhere? Where would it reference it?

Start by deserializing your JSON, and then do something with that data. At this point, you seem to be storing quite a bit in the state - I'm not going to lecture you on functional decomposition of components, but if you're following the pattern set by the rest of your application why not store it there?

R. McManaman
  • 304
  • 2
  • 14
  • this.setState({ isLoading: false, dataSource: responseJson, }, function(){ }); – imanealami Feb 24 '20 at 15:58
  • Are you asking if that would work, or indicating that you've already tried this? That snippet of code isn't in your original post, and I'm at a loss for what you're trying to communicate by commenting it here. – R. McManaman Feb 24 '20 at 16:06
  • Sorry if i made you lost !! but what I want to say is that I know how to save my data (using dataState:responseJson inside setState) but I dont know how to pass this array to another page and show them into table – imanealami Feb 24 '20 at 16:16
  • By page do you mean component? You can pass data between components as props - if this is the parent of your other page, you should be able to follow along here: https://reactjs.org/docs/components-and-props.html – R. McManaman Feb 24 '20 at 16:18