Skip to content

Commit 085792a

Browse files
committed
update docs and example to be update to date
1 parent 058336a commit 085792a

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

README.md

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ const flickityOptions = {
3333
function Carousel() {
3434
return (
3535
<Flickity
36-
className={'carousel'} // default ''
37-
elementType={'div'} // default 'div'
38-
options={flickityOptions} // takes flickity options {}
39-
disableImagesLoaded={false} // default false
40-
reloadOnUpdate // default false
41-
static // default false
36+
className={'carousel'}
37+
elementType={'div'}
38+
options={flickityOptions}
39+
disableImagesLoaded
40+
reloadOnUpdate
41+
static
4242
>
4343
<img src="/images/placeholder.png"/>
4444
<img src="/images/placeholder.png"/>
@@ -50,7 +50,7 @@ function Carousel() {
5050
```
5151
### Example Usage:
5252
See a codesandbox example here:
53-
https://codesandbox.io/s/qlz12m4oj6
53+
https://codesandbox.io/s/react-flickity-demo-wwszqm
5454

5555
See an example with server side rendering:
5656

@@ -67,7 +67,7 @@ https://github.com/theolampert/react-flickity-component-example/tree/typescript
6767
| -------------------- | -----------| --------|---------------------------------------------------------------|
6868
| `className` | `String` | `''` | Applied to top level wrapper |
6969
| `elementType` | `String` | `'div'` | Wrapper's element type |
70-
| `options` | `Object` | `{}` | Flickity initialization opions |
70+
| `options` | `Object` | `{}` | Flickity initialization options |
7171
| `disableImagesLoaded`| `Boolean` | `false` | Disable call `reloadCells` images are loaded |
7272
| `flickityRef` | `Function` | | Like `ref` function, get Flickity instance in parent component|
7373
| `reloadOnUpdate` | `Boolean` | `false` | **Read next section before you set this prop.** Run `reloadCells` and `resize` on `componentDidUpdate` |
@@ -77,18 +77,43 @@ https://github.com/theolampert/react-flickity-component-example/tree/typescript
7777

7878
Under the hood, react-flickity-component uses a [React Portal](https://reactjs.org/docs/portals.html) to render children slides inside a Flickity instance. The need for a portal is because after Flickity is initialized, new DOM nodes (mostly Flickity wrapper elements) would be created, this changes the DOM hierarchy of the parent component, thus any future update, whether it's originated from Flickity, like adding/removing slides, or from parent, like a prop changes, will make React fail to reconcile for the DOM snapshot is out of sync.
7979

80-
#64 introduced a new prop to change the underlying render method: instead of portal, react-flickity-component will directly render children. This is create a smoother server-side rendering experience, but **please be aware using `static` prop possibly will cause all your future update to fail,** which means adding/removing slides will definitely fail to render, so use with caution.
80+
[#64](https://github.com/yaodingyd/react-flickity-component/pull/64) introduced a new prop to change the underlying render method: instead of using portal, react-flickity-component will directly render children. This is to create a smoother server-side rendering experience, but **please be aware using `static` prop possibly will cause all your future update to fail,** which means adding/removing slides will definitely fail to render, so use with caution.
8181

82-
However there is a fail-safe option `reloadOnUpdate`. It means every time there is a update, we tear down and set up Flickity. This will ensure that Flickity is always rendered correctly, but it's a rather costly operation and it will cause a flicker since DOM nodes are destroyed and recreated.
82+
However there is a fail-safe option `reloadOnUpdate`. It means every time there is a update, we tear down and set up Flickity. This will ensure that Flickity is always rendered correctly, but it's a rather costly operation and it **will cause a flicker** since DOM nodes are destroyed and recreated. Please also note it means any update, either rerender of the parent, or any of the props change, will always cause an update in the Flickity component. For more information, see a detailed explanation and workaround in [#147](https://github.com/yaodingyd/react-flickity-component/issues/147).
8383

8484

8585
### Use Flickity's API and events
8686

8787
You can access the Flickity instance with `flickityRef` prop just like `ref`, and use this instance to register events and use API.
8888

8989
```javascript
90+
// function component
91+
function Carousel () {
92+
const ref = React.useRef(null);
9093

94+
function myCustomNext = () {
95+
// You can use Flickity API
96+
ref.current.next()
97+
}
98+
99+
React.useEffect(() => {
100+
if (ref.current) {
101+
ref.current.on("settle", () => {
102+
console.log(`current index is ${ref.current.selectedIndex}`);
103+
});
104+
}
105+
}, []);
91106

107+
return (
108+
<Flickity flickityRef={c => ref.current = c}>
109+
<img src="/images/placeholder.png"/>
110+
<img src="/images/placeholder.png"/>
111+
<img src="/images/placeholder.png"/>
112+
</Flickity>
113+
<Button onClick={myCustomNext}>My custom next button</Button>
114+
)
115+
}
116+
// class component
92117
class Carousel extends React.Component {
93118

94119
componentDidMount = () => {
@@ -114,7 +139,6 @@ class Carousel extends React.Component {
114139
)
115140
}
116141
}
117-
118142
```
119143

120144

0 commit comments

Comments
 (0)