![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenanceādevelopers should switch to Vite or other modern alternatives.
In joinedapp we've run several times into the same problem: how should we deal with forms?
We've tried to use different solutions redux-forms, react-forms. We thought those solutions are still a little bit artifitial.
That's why using the new Context API and hooks, we've developed a new library for everybody who wants to use it.
npm install --save spooner
# peer deps
npm install --save react prop-types
From the beginning, you can use directly the exported components.
import { Form, Input } from 'spooner';
function handleSubmit(value) {
console.log(values); // { email: 'adrian@spooner.io', password: 'myP@ssw0rd' }
}
<Form onSubmit={handleSubmit}>
<Input name="email" type="text" />
<Input name="password" type="password" />
<button type="submit">Send</button>
</Form>
Also, spooner
exports other form components like Select
and Textarea
.
Trying to make it easier to work with different kind of inputs as checkbox
,
file
or radio
; they are also provided within the package.
So whenever you used for example a Checkbox
:
import { Checkbox } from 'spooner';
function handleChange(value, prevValue) {
console.log(value); // true (whenever is checked)
console.log(prevValue); // false
}
<Checkbox name="remember" onChange={handleChange} />
Due to the "duality" of Radio - its value it's provided from the actual value attribute whenever this is checked - we applied a different rule with this input.
Prop value
will be actual value provided from the form
or prop. And content
will be the prop that indicated the new value whenever radio is checked.
import { Radio } from 'spooner';
function handleChange(value) {
console.log(value); // cat, dog or turtle
}
<Radio content="cat" name="pet" onChange={handleChange} />
<Radio content="dog" name="pet" onChange={handleChange} />
<Radio content="turtle" name="pet" onChange={handleChange} />
import React from 'react';
import { Checkbox, Form, Input } from 'spooner';
const Login = () => {
const handleSubmit = value => {
console.log(value); // { password: 'myP@ssw0rd', remember: true, user: 'adrian' }
};
return (
<Form onSubmit={handleSubmit}>
<Input name="user" />
<Input name="password" type="password" />
<Checkbox name="remember" />
<button type="submit">Send</button>
</Form>
);
};
export default Login;
Form
could be a controlled component, as any other controlled component provides
defaultValue
and value
props.
import React from 'react';
import { Checkbox, Form, Input } from 'spooner';
const Login = () => {
const handleSubmit = value => {
console.log(value); // { password: 'myP@ssw0rd', remember: true, user: 'adrian' }
};
return (
<Form defaultValue={{ remember: true }} onSubmit={handleSubmit}>
<Input name="user" />
<Input name="password" type="password" />
<Checkbox name="remember" />{' '}
{/* This checkbox will shown checked by default */}
<button type="submit">Send</button>
</Form>
);
};
export default Login;
As mention before, Form
could be controlled. So if we fill value
prop we need
to update it when is needed.
const Login = () => {
const [value, setValue] = useState({});
const handleChange = nextValue => {
setValue(nextValue);
};
const handleSubmit = () => {
console.log(value);
};
return (
<Form onChange={handleChange} onSubmit={handleSubmit} value={value}>
<Input name="user" />
<Input name="password" type="password" />
<Checkbox name="remember" />
<button type="submit">Send</button>
</Form>
);
};
Same concept could be applied to any of the inputs (or fields) provided within
spooner
.
const Login = () => {
const [value, setValue] = useState({});
const handleChange = nextValue => {
// it will be executed in the first render with (at least) { remember: true }
setValue(nextValue);
};
const handleSubmit = () => {
console.log(value);
};
return (
<Form onChange={handleChange} onSubmit={handleSubmit} value={value}>
<Input name="user" />
<Input name="password" type="password" />
<Checkbox defaultValue={true} name="remember" />
<button type="submit">Send</button>
</Form>
);
};
Fair question, first will not trigger any changes on the Form, and the second will trigger onChange on the form but not in the input (it makes sense because input didn't change its initial value).
There are some times that we want to group fields inside a single key. For that
reason, we provide Fieldset
. In this case, we increase the capabilities of the
fieldset tag to actually group values together.
const ContactInformation = () => {
const handleSubmit = value => {
console.log(value); // { address: { city: 'Madrid', number: '27', street: 'Eloy Gonzalo', zipcode: 28010 }, name: 'Adrian', title: 'mr' }
};
return (
<Form onSubmit={handleSubmit}>
<Select name="title">
<option value="mr">Mister</option>
<option value="mrs">Miss</option>
</Select>
<Input name="name" type="text" />
<Fieldset name="address">
<Input name="street" type="text" />
<Input name="number" type="number" />
<Input name="city" type="text" />
<Input name="zipcode" type="text" />
</Fieldset>
</Form>
);
};
And sometimes we want some values as an array:
const Bill = () => {
const handleSubmit = value => {
console.log(value); // { items: [12, 5, 0] }
};
return (
<Form onSubmit={handleSubmit}>
<Table name="items">
{({ $add, array, index, ...props }) => (
<>
Item #{index}
<Input name="doesn't matter" {...props} type="number" />
{index === array.length && (
<button onClick={() => $add(0)} type="button">
add
</button>
)}
</>
)}
</Table>
<button type="submit">Send</button>
</Form>
);
};
Or maybe together:
const Bill = () => {
const handleSubmit = value => {
console.log(value); // { items: [{ amount: 2, name: 'Socks', price: 10 }, { amount: 1, name: 'Shirt', price: 20 }, { amount: 1, name: 'Pin', price: 0 }] }
};
return (
<Form onSubmit={handleSubmit}>
<Table name="items">
{({ $add, array, index, ...props }) => (
<Fieldset name="whatever" {...props}>
Item #{index}
<Input name="name" type="text" />
<Input name="amount" type="number" />
<Input name="price" type="number" />
{index === array.length && (
<button onClick={() => $add(0)} type="button">
add
</button>
)}
</Fieldset>
)}
</Table>
<button type="submit">Send</button>
</Form>
);
};
This was another major pain point when we researched about forms. How can I validate the information before send it?
Under the hood spooner
is using validate.js.
We liked in a very opinionated way the syntax provided by validate.js
.
Although it seems very verbose in some ocassions, it provides a lot of information
to the reader.
We created a couple of custom validators to provide support for Fieldset
and
Table
fields.
FAQs
Build forms in React as you do in HTML (with some š¬!!!)
The npm package spooner receives a total of 5 weekly downloads. As such, spooner popularity was classified as not popular.
We found that spooner demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Ā It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenanceādevelopers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.