Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
wx-app-hairDryer
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
姜雷
wx-app-hairDryer
Commits
3e7cf103
Commit
3e7cf103
authored
Feb 19, 2019
by
姜雷
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化context的使用
parent
b48daa1f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
18 deletions
+23
-18
App.js
src/containers/App.js
+3
-12
index.js
src/containers/Login/index.js
+0
-1
userinfo-context.js
src/context/userinfo-context.js
+20
-5
No files found.
src/containers/App.js
View file @
3e7cf103
import
React
,
{
useState
}
from
'react'
;
import
{
Switch
,
Route
}
from
'react-router-dom'
;
import
UserInfoContext
,
{
initUserinfo
}
from
'../context/userinfo-context'
;
import
{
UserProvider
}
from
'../context/userinfo-context'
;
import
NavBar
from
'../components/NavBar'
;
import
Index
from
'./Index/Index'
;
...
...
@@ -11,19 +11,10 @@ import BindEquipment from './BindEquipment';
import
NoMatch
from
'./NoMatch'
;
function
App
()
{
const
[
userinfo
,
setUserinfo
]
=
useState
(
initUserinfo
);
const
[
title
,
setTitle
]
=
useState
(
'多彩运维'
);
return
(
<
div
className
=
'App'
>
<
UserInfoContext
.
Provider
value
=
{{
userinfo
:
userinfo
,
updateUserInfo
:
val
=>
setUserinfo
({
...
userinfo
,
...
val
,
}),
}}
>
<
UserProvider
>
<
Route
render
=
{
props
=>
<
NavBar
{...
props
}
title
=
{
title
}
/>} /
>
<
Switch
>
<
Route
...
...
@@ -49,7 +40,7 @@ function App() {
/>
<
Route
component
=
{
NoMatch
}
/
>
<
/Switch
>
<
/User
InfoContext.
Provider
>
<
/UserProvider
>
<
/div
>
);
}
...
...
src/containers/Login/index.js
View file @
3e7cf103
...
...
@@ -21,7 +21,6 @@ export class Index extends React.Component {
}
componentWillMount
()
{
console
.
log
(
this
.
props
);
const
{
setTitle
,
updateUserInfo
}
=
this
.
props
;
setTitle
(
'登陆'
);
const
{
code
}
=
this
.
props
;
...
...
src/context/userinfo-context.js
View file @
3e7cf103
import
React
from
'react'
;
import
React
,
{
createContext
,
useState
}
from
'react'
;
function
is_weixn
()
{
var
ua
=
navigator
.
userAgent
.
toLowerCase
();
...
...
@@ -21,9 +21,24 @@ export const initUserinfo = {
},
};
export
const
UserContext
=
React
.
createContext
({
userinfo
:
initUserinfo
,
updateUserInfo
:
()
=>
{},
});
export
const
UserContext
=
createContext
();
export
const
UserProvider
=
props
=>
{
const
[
userinfo
,
setUserinfo
]
=
useState
(
initUserinfo
);
return
(
<
UserContext
.
Provider
value
=
{{
userinfo
:
userinfo
,
updateUserInfo
:
val
=>
setUserinfo
({
...
userinfo
,
...
val
,
}),
}}
>
{
props
.
children
}
<
/UserContext.Provider
>
);
};
export
default
UserContext
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment