@@ -131,35 +131,22 @@ ReactDOM.render(
131
131
### Uncontrolled Component
132
132
``` js
133
133
import React from ' react' ;
134
+ import ReactDOM from ' react-dom' ;
134
135
import Sortable from ' react-sortablejs' ;
135
136
136
137
class App extends React .Component {
137
138
state = {
138
139
items: [' Apple' , ' Banana' , ' Cherry' , ' Guava' , ' Peach' , ' Strawberry' ]
139
140
};
140
-
141
- sortable = null ;
142
-
143
- handleReverseOrder () {
144
- const order = this .sortable .toArray ();
145
- this .sortable .sort (order .reverse ());
146
- }
141
+
147
142
render () {
148
143
const items = this .state .items .map ((val , key ) => (< li key= {key} data- id= {val}> {val}< / li> ));
149
-
150
- retrun (
144
+
145
+ return (
151
146
< div>
152
- < button type= " button" onClick= {:: this .handleReverseOrder }> Reverse Order< / button>
153
147
< Sortable
154
148
// See all Sortable options at https://github.com/RubaXa/Sortable#options
155
149
options= {{
156
- handle: " .my-handle" , // Drag handle selector within list items
157
- draggable: " .item" // Specifies which items inside the element should be sortable
158
- }}
159
- ref= {(c ) => {
160
- if (c) {
161
- this .sortable = sortable;
162
- }
163
150
}}
164
151
tag= " ul" // Defaults to "div"
165
152
>
@@ -169,44 +156,36 @@ class App extends React.Component {
169
156
);
170
157
}
171
158
}
159
+
160
+ ReactDOM .render (
161
+ < App / > ,
162
+ document .getElementById (' container' )
163
+ );
172
164
```
173
165
174
166
### Controlled Component
175
167
176
168
``` js
177
169
import React from ' react' ;
178
- import Sortable from ' react-sortablejs' ;
170
+ import ReactDOM from ' react-dom' ;
171
+ import Sortable from ' ../src' ;
179
172
180
173
class App extends React .Component {
181
174
state = {
182
175
items: [' Apple' , ' Banana' , ' Cherry' , ' Guava' , ' Peach' , ' Strawberry' ]
183
176
};
184
-
185
- sortable = null ;
186
-
187
- handleReverseOrder () {
188
- const order = this .sortable .toArray ();
189
- this .sortable .sort (order .reverse ());
190
- }
177
+
191
178
render () {
192
179
const items = this .state .items .map ((val , key ) => (< li key= {key} data- id= {val}> {val}< / li> ));
193
-
194
- retrun (
180
+
181
+ return (
195
182
< div>
196
- < button type= " button" onClick= {:: this .handleReverseOrder }> Reverse Order< / button>
197
183
< Sortable
198
184
// See all Sortable options at https://github.com/RubaXa/Sortable#options
199
185
options= {{
200
- handle: " .my-handle" , // Drag handle selector within list items
201
- draggable: " .item" // Specifies which items inside the element should be sortable
202
- }}
203
- ref= {(c ) => {
204
- if (c) {
205
- this .sortable = sortable;
206
- }
207
186
}}
208
187
tag= " ul" // Defaults to "div"
209
- onChange= {(order, sortable) { // [Optional] Controlled Component
188
+ onChange= {(order , sortable ) => {
210
189
this .setState ({ items: order });
211
190
}}
212
191
>
@@ -216,6 +195,11 @@ class App extends React.Component {
216
195
);
217
196
}
218
197
}
198
+
199
+ ReactDOM .render (
200
+ < App / > ,
201
+ document .getElementById (' container' )
202
+ );
219
203
```
220
204
221
205
### Shared Group
0 commit comments