How do capture searched data and add them to registration for posting in React

Viewed 20

searching/fetch is possible but how to get the data from search and select some data and post them...in this case I want to get the _id of student

this is the code but I have not implemented the post method bcz i want first to print the values at the console

    export default function NewParent() {
      const { currentColor } = useStateContext();
      const [isLoading, setLoading] = useState(false);
  const [address, setAddress] = useState('');
  const [email, setEmail] = useState('');
  const [gender, setGender] = useState('');
  const [phone, setPhone] = useState('');
  const [status, setStatus] = useState('Yes');
  const [regNo, setRegNo] = useState('');
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');
  const [student, setStudent] = useState('');
  const [search, setSearch] = useState('');

  const reducer = (state, action) => {
    switch (action.type) {
      case 'FETCH_REQUEST':
        return { ...state, loading: true };
      case 'FETCH_SUCCESS':
        return { ...state, info: action.payload, loading: false };
      case 'FETCH_FAIL':
        return { ...state, loading: false, error: action.payload };
      default:
        return { state };
    }
  };

  const [{ loading, error, info }, dispatch] =   useReducer(logger(reducer), {

    info: [],
    loading: true,
    error: '',
  });

  useEffect(() => {
    const fetchData = async () => {
      dispatch({ type: 'FETCH_REQUEST' });
      try {
        const result = await axios.get(
          // eslint-disable-next-line comma-dangle
          `http://localhost:9000/api/students/regNo/${search}`
        );
        dispatch({ type: 'FETCH_SUCCESS', payload: result.data });
      } catch (err) {
        dispatch({ type: 'FETCH_FAIL', payload: getError(err) });
      }
    };
    fetchData();
  }, [search, dispatch]);

  // useEffect(() => {
  //   setRegNo(info.regNo);
  //   setStudent(info._id);
  // }, [regNo, student]);

  console.log(regNo);
  console.log(student);
}
]]
0 Answers
Related