# django-react-csrftoken

> A drop-in React component for submitting forms with a Django CSRF middleware token.

Latest version **1.0.4** (published 2018-06-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install django-react-csrftoken
pnpm add django-react-csrftoken
yarn add django-react-csrftoken
bun add django-react-csrftoken
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2018-06-14 |
| First published | 2016-10-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 13.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 33 |
| Author | Michael Martinez |
| Maintainers | imichael |
| Keywords | react, django, csrf, token |

## Links

- npm: https://www.npmjs.com/package/django-react-csrftoken
- Repository: https://github.com/iMerica/django-react-csrftoken
- Issues: https://github.com/iMerica/django-react-csrftoken/issues
- npm.io page: https://npm.io/package/django-react-csrftoken

## Dependencies (3)

- [react-cookie](https://npm.io/package/react-cookie.md) ^0.4.8
- [babel-runtime](https://npm.io/package/babel-runtime.md) 6.x.x
- [react-dom-factories](https://npm.io/package/react-dom-factories.md) ^1.0.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2018-06-14
- 1.0.3 — 2017-09-17
- 1.0.2 — 2017-09-04
- 1.0.1 — 2016-10-03
- 1.0.0 — 2016-10-03

## README

# DjangoCSRFToken

A drop-in React component for submitting forms with a Django CSRF middleware token.


## Installation

    npm install --save django-react-csrftoken


## Usage

```javascript

import React from 'react';
import DjangoCSRFToken from 'django-react-csrftoken'

class MyLoginForm extends React.Component {
  render(){
    return (
      <div className="container">
          <form>
            <DjangoCSRFToken/>
            // email
            // password
            // submit button
          </form>
      </div>
    )
  }
}
```

## CSRF Cookie and React

Because react renders elements dynamically, Django might not set a CSRF token cookie if you render a form using react.
This is described in [the Django docs](https://docs.djangoproject.com/en/1.11/ref/csrf/):
> If your view is not rendering a template containing the csrf_token template tag, Django might not set the CSRF token cookie. This is common in cases where forms are dynamically added to the page. To address this case, Django provides a view decorator which forces setting of the cookie: ensure_csrf_cookie().

To fix this problem add the decorator mentioned above to your views:
```python

from django.views.decorators.csrf import ensure_csrf_cookie

@ensure_csrf_cookie
def register_view(request):
    // ...

```

---
_Source: https://npm.io/package/django-react-csrftoken · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
