form prevent default react code example

Example 1: stop from from refresching page react

function handleSubmit(event) {
      event.preventDefault();
    }


	...

	

Example 2: prevent default react

import React from 'react';
 
const initialList = [
  'Learn React',
  'Learn Firebase',
  'Learn GraphQL',
];
 
const ListWithAddItem = () => {
  const [value, setValue] = React.useState('');
  const [list, setList] = React.useState(initialList);
 
  const handleChange = event => {
    setValue(event.target.value);
  };
 
  const handleSubmit = event => {
    if (value) {
      setList(list.concat(value));
    }
 
    setValue('');
 
    event.preventDefault();
  };
 
  return (
    
    {list.map(item => (
  • {item}
  • ))}
); }; export default ListWithAddItem;

Tags:

Misc Example