E
E
EmKotlety2021-09-02 06:20:46
React
EmKotlety, 2021-09-02 06:20:46

How to return a value from a function in react?

You need to return the message value from the registration function call in order to pass it to another component. Based on this message, a hint will pop up on how the request went. In the function itself, I can’t do this, because the tooltip component cannot be displayed there. I need a react function. And you can’t call a react function in the formik callback, it swears ...
Here is the function:

export const registration = async (email, login, password, context, message) => {
    
    try{
       
        const response = await axios.post(`http://localhost:5000/api/auth/register`,
    {email, login, password})
    message = response.data.message
    console.log('fromfuncmessage', message)
    // alert(message)
        if(message === 'Пользователь создан'){
            context.handleClose()
           
            
        }
        return message
    }
    
    catch(e){
        alert(e.response.data.message)
        console.log(e)
    }
    
}

It is called here:
import React from 'react';
import Avatar from '@material-ui/core/Avatar';
import Button from '@material-ui/core/Button';
import CssBaseline from '@material-ui/core/CssBaseline';
import TextField from '@material-ui/core/TextField';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Checkbox from '@material-ui/core/Checkbox';
import Link from '@material-ui/core/Link';
import Grid from '@material-ui/core/Grid';
import Box from '@material-ui/core/Box';
import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import Container from '@material-ui/core/Container';
import { useState , useContext} from 'react'
import { Formik } from 'formik'
import * as yup from 'yup'
import { registration } from './actions/user';
import { useDispatch } from 'react-redux';
import {Context} from './App'
const useStyles = makeStyles((theme) => ({
  wrappper: {
    marginTop: theme.spacing(1),
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
    borderRadius: '10px !important'
  },
  avatar: {
    margin: theme.spacing(1),
    backgroundColor: theme.palette.secondary.main,
  },
  form: {
    width: '100%', // Fix IE 11 issue.
    marginTop: theme.spacing(1),
  },
  submit: {
    margin: theme.spacing(3, 0, 2),
  },
  
}));

export default function RegistrForm(props) {
  const message = 'пусто';
  
  const context = useContext(Context)
  console.log('wtf:', context.handleClose)
  const classes = useStyles();
  const dispatch = useDispatch()
  const validationsShema = yup.object().shape({
    login: yup.string().min(2,'too shot')
    .max(15,'Too long').required('Required'),
    email: yup.string().email('Invalid email').required('Required'),
    password: yup.string().min(2,'пароль не надежный')
    .max(15,'Too long').required('Required'),
  })

  return (
    <Container component="main" maxWidth="xs">
      <CssBaseline />
      <div className={classes.wrappper}>
        <Avatar className={classes.avatar}>
          <LockOutlinedIcon />
        </Avatar>
        <Typography component="h1" variant="h5">
          Sign up
        </Typography>
        
            <Formik 
                validateOnBlur
                initialValues={{
                login:"",
                email: "",
                password: ""
                }}
                // validateOnBlur
                validationSchema = {validationsShema}
                onSubmit = {(values) => registration(values.email,values.login, values.password, context,message)}
                
            >
              
        {({ values, errors, touched, handleChange, handleBlur, isValid, handleSubmit, dirty}) => (
        
        <form className={classes.form} noValidate>
          <TextField
            value = {values.login}
            type = {`text`}S
            name = { `login`}
            onChange = {handleChange}
            onBlur = {handleBlur}
            
            variant="outlined"
            margin="normal"
            required
            fullWidth
            id="login"
            label="Login"
            
            autoComplete="login"
            autoFocus
          />
          {touched.email && errors.email ? <div className = "alert">{errors.login}</div>:<div></div>}
          <TextField
            value = {values.email}
            type = {`text`}
            name = { `email`}
            onChange = {handleChange}
            onBlur = {handleBlur}
            
            variant="outlined"
            margin="normal"
            required
            fullWidth
            id="email"
            label="Email Address"
            
            autoComplete="email"
            
          />
          {touched.email && errors.email ? <div className = "alert">{errors.email}</div>:<div></div>}
          <TextField
            value = {values.password}
            type = {`password`}
            id = "password"
            onChange = {handleChange}
            onBlur = {handleBlur}
            variant="outlined"
            margin="normal"
            required
            fullWidth
            label="Password"
            
            autoComplete="current-password"
          />
          {touched.password && errors.password ? <div className = "alert">{errors.password}</div>:<div></div>}
          <FormControlLabel
            control={<Checkbox value="remember" color="primary" />}
            label="Remember me"
          />
          <Button
            disabled={ !isValid  }
            onClick = {(e)=>{
              handleSubmit(e);
            }}
            type = {`submit`}
            name="action"
            fullWidth
            variant="contained"
            color="primary"
            className={classes.submit}
          >
            Sign Up
          </Button>
          <Grid container>
            <Grid item xs>
              <Link href="#" variant="body2">
                Forgot password?
              </Link>
            </Grid>
            <Grid item>
              <Link href="#" variant="body2">
                {"Don't have an account? Sign Up"}
                {console.log('message:', message)}
              </Link>
            </Grid>
          </Grid>
        </form>
        )}
        </Formik>
      </div>
    </Container>
  );
}

I try to create a variable for a function, I pass it to the callback, but when I output it to the console below, it remains "empty" there

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
ParaBellum577, 2021-09-02
@EmKotlety

create a state.
const [message, setMessage] = React.useState('');
Put a value there and pass it anywhere) This is react)
In the registration function, where you have a return message, write:
setMessage(message);
Done. Your message is in the component's state, pass message wherever you want

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question