Skip to content

Commit 3e2cb51

Browse files
committed
Update the changelog, some slight refactoring before 2.0.
1 parent 13a912f commit 3e2cb51

14 files changed

+143
-122
lines changed

CHANGELOG.md

+34-12
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,54 @@
11
## 2.0.0
22

33
### Breaking changes
4-
4+
55
-The html5 history api is now used by default. This means that you no longer need to provide a `history` object has the first parameter.
66
In practice usually means you will need to drop the first parameter to `withUrlState`, and usage will remain the same.
77

88
#### Before
99

10-
```typescript jsx
11-
const enhance = withUrlState(history, () => ({ color: "blue" }))
10+
```js
11+
const enhance = withUrlState(history, () => ({ color: 'blue' }))
1212
```
1313

14-
1514
#### After
1615

17-
```typescript jsx
18-
const enhance = withUrlState(() => ({ color: "blue" }))
16+
```js
17+
const enhance = withUrlState(() => ({ color: 'blue' }))
1918
```
2019

21-
2220
### New features
2321

22+
A new render-prop based API is also available:
23+
24+
Now you can do:
25+
26+
```js jsx
27+
import { UrlState } from 'with-url-state'
28+
29+
const RenderProp = (props: OwnProps) => (
30+
<UrlState
31+
initialState={{ color: 'purple' }}
32+
render={({ urlState, setUrlState }: UrlStateProps<LiftedState>) => (
33+
<div>
34+
<div>{urlState.color}</div>
35+
<button onClick={() => setUrlState({ color: 'green' })}>Green</button>
36+
<button onClick={() => props.setUrlState({ color: 'purple' })}>Purple</button>
37+
</div>
38+
)}
39+
/>
40+
)
41+
```
42+
2443
`withUrlState` now takes a config object as its second parameter which allows you to:
2544

2645
- Provide a custom `history` object in environments which don't support the html5 api.
27-
- Have more fine grained control over serialisation, such as:
28-
- Defaults which don't appear in the url
29-
- Alias the parameters passed to your component
30-
- Complex nested objects
31-
See the `'supports complex serialisation workflows'` test for more.
3246
- Control weather a change of state should result in a `push` or `replace` event for users navigation control.
47+
48+
- Have more fine grained control over serialisation, such as:
49+
50+
- Defaults which don't appear in the url
51+
- Alias the parameters passed to your component
52+
- Complex nested objects
53+
54+
See the `'supports complex serialisation workflows'` test for more.

Readme.md

+33-30
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@ This package allows applications to retrieve the state from a react component an
1313

1414
## Installation
1515

16-
To install with npm use
16+
To install with npm use
1717

1818
`npm install with-url-state --save`
1919

20-
To install with yarn use
20+
To install with yarn use
2121

2222
`yarn add with-url-state`
2323

2424
## Usage
2525

26-
Check out the [demo](https://dean177.github.io/with-url-state/), the [example/](https://github.com/Dean177/with-url-state/tree/master/example) directory, or play with it in [CodeSandbox](https://codesandbox.io/s/21z35p6pjp).
26+
Play with it in [CodeSandbox](https://codesandbox.io/s/18x4l87yx7) or check out the the [example/](https://github.com/Dean177/with-url-state/tree/master/example)
2727

2828
Using javascript
2929

3030
```javascript
31-
import React from 'react';
31+
import React from 'react'
3232
import { withUrlState } from 'with-url-state'
3333

34-
const enhance = withUrlState((props) => ({ color: 'blue' }))
34+
const enhance = withUrlState(props => ({ color: 'blue' }))
3535

36-
export const UrlForm = enhance((props) => (
36+
export const UrlForm = enhance(props => (
3737
<div className="UrlForm">
38-
<div className="current-state" style={{ backgroundColor: props.urlState.color}}>
38+
<div className="current-state" style={{ backgroundColor: props.urlState.color }}>
3939
<div>{props.urlState.color}</div>
4040
</div>
4141
<div className="color-buttons">
@@ -66,7 +66,7 @@ const enhance = withUrlState<OwnProps, UrlState>((prop: OwnProps) => ({ color: '
6666

6767
export const UrlForm = enhance((props: OwnProps & UrlStateProps<UrlState>) => (
6868
<div className="UrlForm">
69-
<div className="current-state" style={{ backgroundColor: props.urlState.color}}>
69+
<div className="current-state" style={{ backgroundColor: props.urlState.color }}>
7070
<div>{props.urlState.color}</div>
7171
</div>
7272
<div className="color-buttons">
@@ -82,11 +82,9 @@ export const UrlForm = enhance((props: OwnProps & UrlStateProps<UrlState>) => (
8282
</div>
8383
</div>
8484
))
85-
8685
```
8786

88-
Using the renderprop component
89-
87+
Using the renderprop component
9088

9189
```typescript jsx
9290
import React from 'react'
@@ -95,33 +93,38 @@ import { UrlState } from 'with-url-state'
9593
type OwnProps = {}
9694
type UrlState = { color: string }
9795

98-
export const UrlForm = (props: OwnProps) =>
99-
<UrlState initialState={{ color: 'green' }} render={({ setUrlState, urlState }) =>
100-
<div className="UrlForm">
101-
<div className="current-state" style={{ backgroundColor: urlState.color}}>
102-
<div>{urlState.color}</div>
96+
export const UrlForm = (props: OwnProps) => (
97+
<UrlState
98+
initialState={{ color: 'green' }}
99+
render={({ setUrlState, urlState }) => (
100+
<div className="UrlForm">
101+
<div className="current-state" style={{ backgroundColor: urlState.color }}>
102+
<div>{urlState.color}</div>
103+
</div>
104+
<div className="color-buttons">
105+
<button className="Red" onClick={() => setUrlState({ color: 'red' })}>
106+
Red
107+
</button>
108+
<button className="Green" onClick={() => setUrlState({ color: 'green' })}>
109+
Green
110+
</button>
111+
<button className="Blue" onClick={() => setUrlState({ color: 'blue' })}>
112+
Blue
113+
</button>
114+
</div>
103115
</div>
104-
<div className="color-buttons">
105-
<button className="Red" onClick={() => setUrlState({ color: 'red' })}>
106-
Red
107-
</button>
108-
<button className="Green" onClick={() => setUrlState({ color: 'green' })}>
109-
Green
110-
</button>
111-
<button className="Blue" onClick={() => setUrlState({ color: 'blue' })}>
112-
Blue
113-
</button>
114-
</div>
115-
</div>
116-
} />
116+
)}
117+
/>
118+
)
117119
```
118120

119121
## Motivation
120122

121123
`with-url-state` automates the query parameter manipulations, simplifying URL sharing for search results, querying data or tracking a visible portion of a map.
122124

123125
The api provided is:
126+
124127
- based on [higer-order-components](https://reactjs.org/docs/higher-order-components.html) which makes it composable and testable
125128
- has a render-prop alternative for convenience
126-
- type-safe thanks to [Typescript](https://www.typescriptlang.org/)
129+
- type-safe thanks to [Typescript](https://www.typescriptlang.org/)
127130
- very similar to [Reacts built in state](https://reactjs.org/docs/state-and-lifecycle.html) apis, so converting a component which already manages state is usually as simple as replacing `setState` with `setUrlState`!

docs/asset-manifest.json

-6
This file was deleted.

docs/favicon.ico

-3.78 KB
Binary file not shown.

docs/index.html

-1
This file was deleted.

docs/manifest.json

-15
This file was deleted.

docs/service-worker.js

-1
This file was deleted.

docs/static/css/main.7d36581c.css

-2
This file was deleted.

docs/static/css/main.7d36581c.css.map

-1
This file was deleted.

docs/static/js/main.37ae200e.js

-2
This file was deleted.

docs/static/js/main.37ae200e.js.map

-1
This file was deleted.

example/src/App.tsx

+19-22
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,25 @@ export const UrlForm = (props: OwnProps & UrlStateProps<LiftedState>) => (
3131

3232
const Html = withUrlState<LiftedState, OwnProps>(() => ({ color: 'blue' }))(UrlForm)
3333

34-
const history = createBrowserHistory()
35-
36-
const Npm = withUrlState<LiftedState, OwnProps>(() => ({ color: 'red' }), { history })(UrlForm)
37-
38-
const CustomSerialisation = withUrlState<LiftedState, OwnProps>(() => ({ color: 'green' }), {
39-
serialisation: {
40-
parse: (search: string) => ({
41-
color:
42-
search === '?c=blue'
43-
? 'blue'
44-
: search === '?c=green'
45-
? 'green'
46-
: search === '?c=red'
47-
? 'red'
48-
: 'blue',
49-
}),
50-
stringify: ({ color }: Partial<LiftedState>) =>
51-
color === 'blue' ? '?c=blue' : color === 'green' ? '?c=green' : '?c=red',
34+
const CustomSerialisation = withUrlState<LiftedState, OwnProps>(
35+
() => ({ color: 'green' }),
36+
{
37+
serialisation: {
38+
parse: (search: string) => ({
39+
color:
40+
search === '?c=blue'
41+
? 'blue'
42+
: search === '?c=green'
43+
? 'green'
44+
: search === '?c=red'
45+
? 'red'
46+
: 'blue',
47+
}),
48+
stringify: ({ color }: Partial<LiftedState>) =>
49+
color === 'blue' ? '?c=blue' : color === 'green' ? '?c=green' : '?c=red',
50+
},
5251
},
53-
})(UrlForm)
52+
)(UrlForm)
5453

5554
const RenderProp = (props: OwnProps) => (
5655
<UrlState
@@ -64,14 +63,12 @@ export default () => (
6463
<div className="App">
6564
<div className="side-nav">
6665
<Link to="/html5">Html5 History</Link>
67-
<Link to="/npm">Npm History</Link>
6866
<Link to="/custom-serialisation">Custom serialisation</Link>
6967
<Link to="/render-prop">Render prop</Link>
7068
</div>
7169
<div className="content">
7270
<Router>
73-
<Html default={true} path="/html5" />
74-
<Npm path="/npm" />
71+
<Html default={true} path="/" />
7572
<CustomSerialisation path="/custom-serialisation" />
7673
<RenderProp path="/render-prop" />
7774
</Router>

0 commit comments

Comments
 (0)