Search code examples
javascriptreactjsapireduxquery-string

how to pass parametrs to api?reactjs


here is my target .....the values come from login (it can be anything)-> for ex email:'[email protected]' password:'12345678'

i should take username until "@" and pass it to api as a username.

export const loginUser = (values, history, setFieldError, setSubmitting) => {
    console.log("values from login", values)

    //here i take the email split value and take as user name 
    const username = values.email.split("@")[0]
    console.log("user name", username)

     //now i should pass the username as a name parametr to api

    return () => {
        axios.get('https://api.agify.io/?', values
        ).then((response) => {
            //if res ok should redirect it 
            console.log("response", response)
            // history.push("/user")
        }).catch(error => console.error(error))
        setSubmitting(false);
    }
}

if it can help here is my link https://codesandbox.io/s/charming-jang-py0mu?file=/src/auth/actions/userActions.js


Solution

  • I think this can do the job (using template literals) :

    axios.get(`https://api.agify.io/?name=${username}`)