Reactjs 将React转换为React Native
我有一个React应用程序,需要转换为React Native。我在web开发方面没有太多经验,因此我可能缺少一些必要的东西。我的问题是,当我在emulator中打开应用程序时,我只看到一个白色页面,上面写着“主页”。这是我到目前为止翻译的代码 index.jsReactjs 将React转换为React Native,reactjs,react-native,Reactjs,React Native,我有一个React应用程序,需要转换为React Native。我在web开发方面没有太多经验,因此我可能缺少一些必要的东西。我的问题是,当我在emulator中打开应用程序时,我只看到一个白色页面,上面写着“主页”。这是我到目前为止翻译的代码 index.js import {AppRegistry} from 'react-native'; import App from './App'; import {name as appName} from './app.json'; AppReg
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);
App.js
import React from 'react';
import Routes from './src/core/Routes';
import configureStore from './src/services/store';
import {Provider} from 'react-redux';
import {View} from 'react-native';
import {createMuiTheme, MuiThemeProvider} from '@material-ui/core';
import {
font_family_default,
font_family_secondary,
font_size_md,
font_size_sm,
font_weight_bold,
font_weight_normal,
font_weight_semi_bold,
} from './src/assets/style/variables';
import {rem} from './src/assets/style/functions';
import colors from './src/assets/style/colors';
const theme = createMuiTheme({
typography: {
fontFamily: font_family_default,
fontSize: 16,
fontWeightLight: font_weight_normal,
fontWeightRegular: font_weight_semi_bold,
fontWeightMedium: font_weight_bold,
},
overrides: {
MuiMenuItem: {
root: {
fontSize: font_size_sm,
fontFamily: font_family_secondary,
fontWeight: font_weight_semi_bold,
minHeight: rem(40),
},
},
MuiMenu: {
paper: {
boxShadow: '0px 12px 56px -8px rgba(0,0,0,0.2)',
borderRadius: `${rem(3)}`,
},
},
MuiExpansionPanelSummary: {
root: {
padding: `0px ${rem(16)}`,
},
content: {
margin: `${rem(8)} 0px `,
},
},
MuiInputBase: {
root: {
fontSize: font_size_md,
},
},
MuiPickersToolbar: {
toolbar: {
backgroundColor: colors.grey.primary,
},
},
MuiPickersDay: {
daySelected: {
backgroundColor: colors.yellow.dark2,
'&:hover': {
backgroundColor: colors.yellow.dark2,
},
},
},
MuiPickersClockNumber: {
clockNumberSelected: {backgroundColor: colors.yellow.dark2},
},
MuiPickersClock: {
pin: {backgroundColor: colors.yellow.dark2},
},
MuiPickersClockPointer: {
pointer: {backgroundColor: colors.yellow.dark2},
thumb: {border: `14px solid ${colors.yellow.dark2}`},
},
},
});
const store = configureStore();
const App: () => React$Node = () => {
return (
<Provider store={store}>
<MuiThemeProvider theme={theme}>
<View style={{flex: 1}}>
<Routes/>
</View>
</MuiThemeProvider>
</Provider>
);
};
export default App;
从“React”导入React;
从“/src/core/Routes”导入路由;
从“/src/services/store”导入configureStore;
从'react redux'导入{Provider};
从“react native”导入{View};
从'@material ui/core'导入{createMuiTheme,MuiThemeProvider};
进口{
字体\u族\u默认值,
font_family_secondary,
字体大小,
字体大小,
字体(粗体),,
字体(重量)(正常),,
字体(粗体)(半粗体),,
}来自“./src/assets/style/variables”;
从“./src/assets/style/functions”导入{rem};
从“/src/assets/style/colors”导入颜色;
const theme=createMuiTheme({
排版:{
fontFamily:font_family_默认值,
尺寸:16,
fontWeightLight:font_weight_normal,
fontWeightRegular:font_-weight_-semi_-bold,
fontWeightMedium:font_-weight_-bold,
},
覆盖:{
MuiMenuItem:{
根目录:{
字体大小:字体大小,
fontFamily:font_family_secondary,
fontWeight:font_-weight_-semi_-bold,
最小身高:雷姆(40),
},
},
MuiMenu:{
论文:{
盒子阴影:“0px 12px 56px-8px rgba(0,0,0,0.2)”,
边界半径:`${rem(3)}`,
},
},
MUIEExpansionPanelSummary:{
根目录:{
填充:`0px${rem(16)}`,
},
内容:{
边距:${rem(8)}0px`,
},
},
MuiInputBase:{
根目录:{
字体大小:字体大小,
},
},
MuiPickersToolbar:{
工具栏:{
背景颜色:colors.grey.primary,
},
},
MuiPickersDay:{
所选日期:{
背景颜色:colors.yellow.dark2,
“&:悬停”:{
背景颜色:colors.yellow.dark2,
},
},
},
MuiPickersClockNumber:{
clockNumberSelected:{backgroundColor:colors.yellow.dark2},
},
MuipickerClock:{
pin:{backgroundColor:colors.yellow.dark2},
},
MuiPickersClockPointer:{
指针:{backgroundColor:colors.yellow.dark2},
拇指:{边框:`14px实心${colors.yellow.dark2}`,
},
},
});
const store=configureStore();
常量应用程序:()=>React$Node=()=>{
返回(
);
};
导出默认应用程序;
Routes.js
import React from 'react';
import {Router, Scene} from 'react-native-router-flux';
import Login from '../pages/login';
const Routes = () => (
<Router>
<Scene key="root">
<Scene key="home" component={Login} title="Home" initial={true}/>
</Scene>
</Router>
);
export default Routes;
从“React”导入React;
从“react native Router flux”导入{Router,Scene};
从“../pages/Login”导入登录名;
常数路由=()=>(
);
导出默认路径;
/src/page/login/index.js
import React, {useState} from 'react';
import styled from 'styled-components/native';
import Input from '../../shared/components/Input';
import Button from '../../shared/components/Button';
import {connect} from 'react-redux';
import {APP_ROUTES} from '../../core/global-constants';
import {Link, Redirect} from 'react-router-native';
import Logo from '../../assets/img/log.svg';
import colors from '../../assets/style/colors';
import {rem} from '../../assets/style/functions';
import {device, font_size_md, font_size_xxl, font_weight_normal} from '../../assets/style/variables';
import {login} from '../../services/actions/authenticationActions';
import useForm from '../../shared/hooks/useForm';
import validate from './loginFormValidations';
const theme = {
backgroundColor: colors.white.light1,
};
function Login(props) {
const {values, handleChange, handleSubmit, errors} = useForm(login, validate);
const [redirect, setRedirect] = useState(false);
const [errorLogin, setErrorLogin] = useState(false);
function login() {
if (Object.keys(errors).length === 0) {
props.login(values).then(
res => {
setRedirect(true);
},
err => {
errors.login = props.errorMessage
? `${props.errorMessage}, please try again`
: `No active account found with the given credentials, please try again`;
setErrorLogin(true);
},
);
}
}
if (redirect) {
return <Redirect to={APP_ROUTES.timeTracker}/>;
}
return (
<Style.Section>
<Style.Img src={Logo} alt="toki timer logo"/>
<Style.Hero>
<Style.Title>Welcome back!</Style.Title>
<Style.Subtitle>Log in to continue</Style.Subtitle>
</Style.Hero>
<Style.Form onSubmit={handleSubmit}>
<Style.FormGroup>
<Input
id="username"
name="username"
placeholder="Username"
backgroundColor={theme}
onChange={handleChange}
value={values.username || ''}
hasError={errors.username}
/>
{errors.username && <Style.ErrorMessage>{errors.username}</Style.ErrorMessage>}
</Style.FormGroup>
<Style.FormGroup>
<Input
id="password"
name="password"
type="password"
placeholder="Password"
backgroundColor={theme}
onChange={handleChange}
value={values.password || ''}
hasError={errors.password}
/>
{errors.password && <Style.ErrorMessage>{errors.password}</Style.ErrorMessage>}
{errorLogin && <Style.ErrorMessage>{errors.login}</Style.ErrorMessage>}
</Style.FormGroup>
<Button name="Login" type="submit"/>
</Style.Form>
<Style.Hero>
<Style.BottomLinks>
<Style.Link to={APP_ROUTES.forgotPassword}>Forgot your password?</Style.Link>
</Style.BottomLinks>
</Style.Hero>
</Style.Section>
);
}
const mapStateToProps = state => {
return {
loading: state.authorization.loading,
isAuthenticated: state.authorization.isAuthenticated,
loginSuccess: state.authorization.loginSuccess,
loginError: state.authorization.loginError,
errorMessage: state.authorization.errorMessage,
isAdmin: state.authorization.userInSession.isAdmin,
};
};
const mapDispatchToProps = {login};
export default connect(
mapStateToProps,
mapDispatchToProps,
)(Login);
const Style = {};
Style.Section = styled.SectionList`
background-color: ${colors.grey.primary};
min-height: 100%;
margin: 0px auto;
display: flex;
flex-direction: column;
`;
Style.Form = styled.View`
display: flex;
flex-direction: column;
justify-content: space-between;
padding: 0 35%;
margin-bottom: ${rem(40)};
@media ${device.mobileS} {
padding: 0 5%;
}
@media ${device.mobileL} {
padding: 0 15%;
}
@media ${device.laptop} {
padding: 0 35%;
}
`;
Style.Hero = styled.View`
display: flex;
flex-direction: column;
margin-bottom: ${rem(70)};
align-items: center;
`;
Style.Title = styled.Text`
color: ${colors.white.primary};
font-size: ${font_size_xxl};
font-weight: ${font_weight_normal};
margin: 0;
`;
Style.Subtitle = styled(Style.Title)`
font-size: ${font_size_md};
`;
Style.Img = styled.Image`
margin-bottom: ${rem(60)};
margin-top: ${rem(60)};
`;
Style.BottomLinks = styled.Text`
color: ${colors.white.primary};
margin-top: ${rem(10)};
`;
Style.Link = styled(Link)`
color: ${colors.yellow.primary};
&:hover {
text-decoration: underline;
}
`;
Style.FormGroup = styled.View`
margin-bottom: ${rem(45)};
`;
Style.ErrorMessage = styled.Text`
font-size: ${font_size_md};
color: ${colors.red.primary};
`;
import React,{useState}来自“React”;
从“样式化组件/本机”导入样式化;
从“../../shared/components/Input”导入输入;
从“../../shared/components/Button”导入按钮;
从'react redux'导入{connect};
从“../../core/global constants”导入{APP_ROUTES};
从“react router native”导入{Link,Redirect};
从“../../assets/img/log.svg”导入徽标;
从“../../assets/style/colors”导入颜色;
从“../../assets/style/functions”导入{rem};
从“../../assets/style/variables”导入{device,font_size_md,font_size_xxl,font_weight_normal}”;
从“../../services/actions/authenticationActions”导入{login};
从“../../shared/hooks/useForm”导入useForm;
从“/loginFormValidations”导入验证;
常量主题={
背景颜色:colors.white.light1,
};
功能登录(道具){
const{values,handleChange,handleSubmit,errors}=useForm(登录,验证);
const[redirect,setRedirect]=useState(false);
const[errorLogin,setErrorLogin]=useState(false);
函数登录(){
if(Object.keys(errors).length==0){
登录(值)。然后(
res=>{
setRedirect(true);
},
错误=>{
errors.login=props.errorMessage
?`${props.errorMessage},请重试`
:`找不到具有给定凭据的活动帐户,请重试';
setErrorLogin(true);
},
);
}
}
如果(重定向){
返回;
}
返回(
欢迎回来!
登录以继续
{errors.username&&{errors.username}
{errors.password&&{errors.password}
{errorLogin&&{errors.login}
忘记密码了?
);
}
常量mapStateToProps=状态=>{
返回{
加载:state.authorization.loading,
isAuthenticated:state.authorization.isAuthenticated,
登录成功:state.authorization.loginsucess,
登录错误:state.authorization.login错误,
errorMessage:state.authorization.errorMessage,
isAdmin:state.authorization.userInSession.isAdmin,
};
};
常量mapDispatchToProps={login};
导出默认连接(
MapStateTops,
mapDispatchToProps,
)(登入);
常量样式={};
Style.Section=styled.SectionList`
背景色:${colors.grey.primar
import React from 'react'
import { Switch, Route } from 'react-router-dom'
import { APP_ROUTES } from './global-constants'
import Login from '../pages/login'
import Dashboard from '../pages/dashboard'
import TimeTracker from '../pages/time-tracker'
import SignUp from '../pages/signUp'
import PrivateRoute from '../shared/components/PrivateRoute'
import UserList from '../pages/user-list'
import DepartmentsList from '../pages/departments-list'
import Reports from '../pages/reports'
import UserProfile from '../pages/user-profile'
import ForgotPassword from '../pages/forgot-password'
import NotFound from '../shared/components/NotFound'
const Routes = () => (
<main>
<Switch>
<Route exact path={[APP_ROUTES.home, APP_ROUTES.login]} component={Login} />
<Route exact path={APP_ROUTES.signUp} component={SignUp} />
<Route exact path={APP_ROUTES.forgotPassword} component={ForgotPassword} />
<PrivateRoute
exact
path={[APP_ROUTES.dashboard, APP_ROUTES.dashboardDeparment]}
component={Dashboard}
/>
<PrivateRoute exact path={APP_ROUTES.timeTracker} component={TimeTracker} />
<PrivateRoute exact path={APP_ROUTES.usersList} component={UserList} />
<PrivateRoute exact path={APP_ROUTES.departments} component={DepartmentsList} />
<PrivateRoute exact path={APP_ROUTES.reports} component={Reports} />
<PrivateRoute exact path={APP_ROUTES.userDetails} component={UserProfile} />
<PrivateRoute component={NotFound} />
</Switch>
</main>
)
export default Routes