Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持hooks全家桶、React.creatContext、React.memo方法转换 #81

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions examples/HelloWorldRN/src/a/Hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, {useState, useEffect} from 'react'
import {View, Button, Text} from 'react-native'
import styles from './styles';

export default function Hooks() {
const [count, setCount] = useState(0)
useEffect(() => {
console.log('Hooks did Mount!')
}, [])

return (
<View style={[styles.item, {flexDirection: 'row', justifyContent: 'center', alignItems: 'center'}]}>
<Button onPress={() => setCount(count + 1)} title="+" />
<View>
<Text>count: {count}</Text>
</View>
<Button onPress={() => setCount(count => count - 1)} title="-" />
</View>
)
}
4 changes: 4 additions & 0 deletions examples/HelloWorldRN/src/a/MyContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {View, Text} from 'react-native'
import styles from './styles';
import ThemeContext from './ThemeContext'

export default class MyContext extends Component {

Expand All @@ -14,6 +15,9 @@ export default class MyContext extends Component {
return (
<View style={[styles.item, {borderBottomWidth: 0}]}>
<Text style={styles.itemText}>{this.context.color}{this.props.name}{this.props.age}</Text>
<ThemeContext.Consumer>
{value => <Text>{value.theme}</Text>}
</ThemeContext.Consumer>
</View>
)
}
Expand Down
4 changes: 3 additions & 1 deletion examples/HelloWorldRN/src/a/MyFunComp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
import {View, Text, StyleSheet} from 'react-native'
import styles from './styles';

export default function MyFunComp({name, age}, {color}) {
function MyFunComp({name, age}, {color}) {

return <View style={styles.item}>
<Text style={styles.itemText}>{name}{age}{color}</Text>
Expand All @@ -13,3 +13,5 @@ export default function MyFunComp({name, age}, {color}) {
MyFunComp.contextTypes = {
color: PropTypes.string,
}

export default React.memo(MyFunComp)
5 changes: 5 additions & 0 deletions examples/HelloWorldRN/src/a/ThemeContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {createContext} from 'react'

const ThemeContext = createContext({theme: 'red'})

export default ThemeContext
14 changes: 10 additions & 4 deletions examples/HelloWorldRN/src/a/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import MyChildComp from './MyChildComp'
import MyStyleComp from './MyStyleComp'
import PlatformComp from './PlatformComp'
import MyHoc from './MyHoc'
import ThemeContext from './ThemeContext'
import Hooks from './Hooks'

import {Dic1, Dic21, Dic22, Dic3, Dic31, Dic32, DicFunc, DF3, DF1, DF2, DF4} from './dic'

Expand Down Expand Up @@ -140,6 +142,8 @@ export default class A extends Component {
age: 18
},

theme: 'yellow',

hasZ: false
}

Expand All @@ -159,7 +163,7 @@ export default class A extends Component {
item2 = <View style={styles.item}><Text style={styles.itemText}>item2</Text></View>

render() {
const {toggleClicked1, arr, user, count} = this.state
const {toggleClicked1, arr, user, count, theme} = this.state

const item1 = <View style={styles.item}><Text style={styles.itemText}>item1</Text></View>
return (
Expand Down Expand Up @@ -226,9 +230,11 @@ export default class A extends Component {
}
</View>


<MyContext {...user}/>

<Hooks />

<ThemeContext.Provider value={{theme}}>
<MyContext {...user}/>
</ThemeContext.Provider>

<View style={styles.button}>
<Button
Expand Down
6 changes: 6 additions & 0 deletions fixtures/hooks/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

[android]
target = Google Inc.:Google APIs:23

[maven_repositories]
central = https://repo1.maven.org/maven2
69 changes: 69 additions & 0 deletions fixtures/hooks/.flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[ignore]
; We fork some components by platform
.*/*[.]android.js

; Ignore "BUCK" generated dirs
<PROJECT_ROOT>/\.buckd/

; Ignore unexpected extra "@providesModule"
.*/node_modules/.*/node_modules/fbjs/.*

; Ignore duplicate module providers
; For RN Apps installed via npm, "Libraries" folder is inside
; "node_modules/react-native" but in the source repo it is in the root
.*/Libraries/react-native/React.js

; Ignore polyfills
.*/Libraries/polyfills/.*

; Ignore metro
.*/node_modules/metro/.*

[include]

[libs]
node_modules/react-native/Libraries/react-native/react-native-interface.js
node_modules/react-native/flow/

[options]
emoji=true

esproposal.optional_chaining=enable
esproposal.nullish_coalescing=enable

module.system=haste
module.system.haste.use_name_reducers=true
# get basename
module.system.haste.name_reducers='^.*/\([a-zA-Z0-9$_.-]+\.js\(\.flow\)?\)$' -> '\1'
# strip .js or .js.flow suffix
module.system.haste.name_reducers='^\(.*\)\.js\(\.flow\)?$' -> '\1'
# strip .ios suffix
module.system.haste.name_reducers='^\(.*\)\.ios$' -> '\1'
module.system.haste.name_reducers='^\(.*\)\.android$' -> '\1'
module.system.haste.name_reducers='^\(.*\)\.native$' -> '\1'
module.system.haste.paths.blacklist=.*/__tests__/.*
module.system.haste.paths.blacklist=.*/__mocks__/.*
module.system.haste.paths.blacklist=<PROJECT_ROOT>/node_modules/react-native/Libraries/Animated/src/polyfills/.*
module.system.haste.paths.whitelist=<PROJECT_ROOT>/node_modules/react-native/Libraries/.*

munge_underscores=true

module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub'

module.file_ext=.js
module.file_ext=.jsx
module.file_ext=.json
module.file_ext=.native.js

suppress_type=$FlowIssue
suppress_type=$FlowFixMe
suppress_type=$FlowFixMeProps
suppress_type=$FlowFixMeState

suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)?:? #[0-9]+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError

[version]
^0.92.0
1 change: 1 addition & 0 deletions fixtures/hooks/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pbxproj -text
56 changes: 56 additions & 0 deletions fixtures/hooks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
*.keystore

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/

*/fastlane/report.xml
*/fastlane/Preview.html
*/fastlane/screenshots

# Bundle artifact
*.jsbundle
1 change: 1 addition & 0 deletions fixtures/hooks/.watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
14 changes: 14 additions & 0 deletions fixtures/hooks/__tests__/App-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @format
*/

import 'react-native';
import React from 'react';
import App from '../App';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
renderer.create(<App />);
});
4 changes: 4 additions & 0 deletions fixtures/hooks/alita.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
entry: './src/index.js',
output: './wx-dist',
}
4 changes: 4 additions & 0 deletions fixtures/hooks/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "xxCompAndChildren",
"displayName": "xxCompAndChildren"
}
18 changes: 18 additions & 0 deletions fixtures/hooks/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function isTargetMp(caller) {
return caller && caller.name === 'babel-loader' && caller.target === 'mini-program'
}

module.exports = function (api) {
const isMp = api.caller(isTargetMp);

api.cache(true);

if (isMp) {
return {
presets: [require('babel-preset-react-app')],
}
}
return {
presets: [require('metro-react-native-babel-preset')],
}
}
5 changes: 5 additions & 0 deletions fixtures/hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {AppRegistry} from 'react-native';
import App from './src/index';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);
17 changes: 17 additions & 0 deletions fixtures/hooks/metro.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Metro configuration for React Native
* https://github.com/facebook/react-native
*
* @format
*/

module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
29 changes: 29 additions & 0 deletions fixtures/hooks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "xxCompAndChildren",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
"link:bc": "yarn link @areslabs/alita-core @areslabs/wx-react @areslabs/wx-react-native"
},
"dependencies": {
"@areslabs/router": "^2.5.0",
"@areslabs/wx-animated": "^2.5.0",
"babel-preset-react-app": "^9.1.2",
"react": "16.8.3",
"react-native": "0.59.9"
},
"devDependencies": {
"@areslabs/alita-weixin-runtime": "^2.5.0",
"@babel/core": "^7.9.0",
"@babel/runtime": "^7.9.2",
"babel-jest": "^25.4.0",
"jest": "^25.4.0",
"metro-react-native-babel-preset": "^0.59.0",
"react-test-renderer": "16.8.3"
},
"jest": {
"preset": "react-native"
}
}
40 changes: 40 additions & 0 deletions fixtures/hooks/src/Callback1.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { useState, useCallback, useMemo } from 'react'
import { View, Text, Button } from 'react-native'

import ChildMemo from './Child1'

const Callback1 = () => {
const [count, setCount] = useState(0)
const [count1, setCount1] = useState(0)
const [name, setName] = useState('Child组件')

const onClick = useCallback((newName: string) => setName(newName + count1), [
count1
])
const newName = useMemo(
() => ({ name, color: name.indexOf('name') !== -1 ? 'red' : 'green' }),
[name]
)

return (
<View>
<Button
onPress={() => {
setCount(count + 1)
}}
title="count-加1"
/>
<Button
onPress={() => {
setCount1(count1 + 1)
}}
title="count1-加1"
/>
<Text>count:{count}</Text>
<Text>count1:{count1}</Text>
<ChildMemo name={newName} onClick={onClick} />
</View>
)
}

export default Callback1
Loading