1.22.1 • Published 5 months ago

react-parse-csv v1.22.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

react-parse-csv

  • Superfast CSV parser (uses papaparse internally).
  • Validate your data using zod
  • Declarative and automatic. Simply call parse() the rest is automatic.
  • Completely headless. Define your own styles!

Install

yarn

yarn add react-parse-csv papaparse zod

npm

npm install react-parse-csv papaparse zod

Example

import {useCsvParser, zodResolver} from "react-parse-csv";
import React, { useCallback } from "react";
import {FormEventHandler} from "react";
import { z } from "zod";

// Define a schema where each row should validate against:
const schema = z.object({
  name: z.string().min(1),
  address: z.string().min(1),
})

const MyComponent: React:FC = () => {
  // Initialize hook.
  const {data, errors, isValid, parse} = useCsvParser({resolver: zodResolver(schema)})

  // Simple filechange handler:
  const handleFileChange:FormEventHandler = useCallback((event) => {
    parse(event.currentTarget.files[0])  
  }, [parse])
   
  return (<>
    <input type="file" onChange={handleFileChange}/>

    {/* All rows were valid: */}
    { isValid && (
      <table>
        { data.map(({name, address}, index) => (
          <tr key={index}>
              <td>{ name }</td>
              <td>{ address }</td>
          </tr>
        ) }    
      </table>
    ) }

    {/* Some rows were invalid: */}
    { !isValid && (
      <table>
        { errors.map(({line, column, message, value}, index) => (
          <tr key={index}>
              <td>{ line }</td>
              <td>{ column }</td>
              <td>{ message }</td>
              <td>{ value }</td>
          </tr>
        ) }
      </table>
    ) }
  </>)
}
1.22.1

5 months ago

1.19.0

1 year ago

1.21.0

1 year ago

1.22.0

1 year ago

1.20.0

1 year ago

1.18.0

2 years ago

1.17.6

2 years ago

1.17.5

2 years ago

1.17.4

2 years ago

1.17.2

2 years ago

1.17.1

2 years ago

1.17.3

2 years ago

1.17.0

3 years ago

1.16.0

3 years ago

1.15.3

3 years ago

1.15.2

3 years ago

1.15.0

3 years ago

1.14.0

3 years ago

1.15.1

3 years ago

1.13.0

3 years ago

1.12.0

3 years ago

1.9.0

3 years ago

1.8.0

3 years ago

1.11.0

3 years ago

1.10.0

3 years ago

1.7.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.0.0

3 years ago