Skip to content

Commit 6995c32

Browse files
authored
Fix/crash node view (#367)
* Added watch mode * fix: crash in nodeview on tap * chore: add shell directive * chore: remove unnecessary ifs
1 parent cfad400 commit 6995c32

File tree

6 files changed

+74
-17
lines changed

6 files changed

+74
-17
lines changed

.template.watchmanconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"ignore_dirs": [
3+
".git",
4+
"node_modules",
5+
".github",
6+
".husky",
7+
".vscode",
8+
"ios",
9+
"android",
10+
"coverage",
11+
"coverage-ts",
12+
"lib",
13+
"storybook"
14+
]
15+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,6 @@ I have moved the roadmap of this library to this trello board so that it can be
8484

8585
[Roadmap Trello Board](https://trello.com/b/IOMR8gFw)
8686

87+
## Development
8788

89+
(Pending to update)

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
"name": "react-native-nested-listview",
33
"version": "0.11.0",
44
"description": "Nested Listview for React native",
5-
"main": "./dist/index.js",
6-
"types": "./dist/index.d.ts",
5+
"main": "src/index.ts",
6+
"types": "src/index.ts",
77
"scripts": {
88
"test": "jest",
99
"test-coverage": "jest && codecov",
1010
"lint": "eslint ./src --ext .js,.jsx,.ts,.tsx",
1111
"type-check": "tsc --noEmit",
1212
"prepublishOnly": "tsc -p ./ --outDir dist/",
13-
"add-react": "yarn add react@16.9.0 && yarn add react-native@0.61.4",
14-
"remove-react": "yarn remove react && yarn remove react-native",
1513
"prettier-check": "prettier --check 'src/**/*.{ts, tsx}'",
1614
"prettier-format": "prettier --write 'src/**/*.{ts, tsx}'",
17-
"check-all": "yarn lint && yarn type-check && yarn test"
15+
"check-all": "yarn lint && yarn type-check && yarn test",
16+
"start-watch": "./start-watch.sh",
17+
"stop-watch": "./stop-watch.sh"
1818
},
1919
"repository": {
2020
"type": "git",

src/NodeView.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { ReactElement, useCallback, useEffect, useState } from 'react';
22
import isEqual from 'react-fast-compare';
33
import { FlatList, Pressable } from 'react-native';
4-
import globalHook from 'use-global-hook';
4+
import globalHook, { Store } from 'use-global-hook';
55

66
export interface INode {
77
_internalId: string;
@@ -31,21 +31,31 @@ export interface IState {
3131
opened: boolean;
3232
}
3333

34+
interface GlobalState {
35+
nodesState: { root: boolean };
36+
}
37+
38+
interface NodeActions {
39+
setOpenedNode: (store: any, { internalId, opened }: any) => void;
40+
}
41+
3442
const actions = {
35-
setOpenedNode: (store: any, { internalId, opened }: any) => {
43+
setOpenedNode: (
44+
store: Store<GlobalState, NodeActions>,
45+
{ internalId, opened }: any,
46+
) => {
3647
store.setState({
3748
nodesState: { ...store.state.nodesState, [internalId]: opened },
3849
});
3950
},
4051
};
4152

42-
const useGlobal = globalHook(
43-
React,
44-
{
45-
nodesState: { root: true },
46-
},
47-
actions,
48-
);
53+
const initialState: GlobalState = {
54+
nodesState: { root: true },
55+
};
56+
57+
// @ts-ignore
58+
const useGlobal = globalHook<GlobalState, NodeActions>(initialState, actions);
4959

5060
const NodeView = React.memo(
5161
({
@@ -62,8 +72,8 @@ const NodeView = React.memo(
6272
const [_node, setNode]: [INode, any] = useState({
6373
...node,
6474
opened:
65-
keepOpenedState && globalState.nodesState[node._internalId]
66-
? !!globalState.nodesState[node._internalId]
75+
keepOpenedState && globalState?.nodesState[node._internalId]
76+
? !!globalState?.nodesState[node._internalId]
6777
: !!node.opened,
6878
});
6979

@@ -116,7 +126,7 @@ const NodeView = React.memo(
116126
const nodeChildren: [] = _node[nodeChildrenName];
117127

118128
const isNodeOpened =
119-
(keepOpenedState && globalState.nodesState[node._internalId]) ||
129+
(keepOpenedState && globalState?.nodesState[node._internalId]) ||
120130
_node.opened;
121131

122132
return (

start-watch.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
kill -9 $(ps -A | grep '[r]eact-native/scripts/../cli.js' | awk '{print $1}')
4+
5+
mv .template.watchmanconfig .watchmanconfig
6+
7+
watchman watch "$(npm root -g)/wml/src"
8+
9+
wml add ./ ../react-native-nested-listview-examples/node_modules/react-native-nested-listview
10+
11+
json -I -f package.json -e "this.main=\"src/index.ts\""
12+
json -I -f package.json -e "this.types=\"src/index.ts\""
13+
14+
wml start

stop-watch.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
kill -9 $(ps -A | grep '[r]eact-native/scripts/../cli.js' | awk '{print $1}')
4+
kill -9 $(ps -A | grep '[w]ml' | awk '{print $1}')
5+
6+
mv .watchmanconfig .template.watchmanconfig
7+
8+
wml rm all
9+
10+
watchman watch-del-all
11+
12+
git checkout -- package.json
13+
14+
rm -rf ../react-native-nested-listview-examples/node_modules
15+
16+
cd .. && cd react-native-nested-listview-examples && yarn install

0 commit comments

Comments
 (0)