Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 重新匹配:注册时将状态设置为有效负载的状态_Firebase_React Native_Redux_React Redux_Rematch - Fatal编程技术网

Firebase 重新匹配:注册时将状态设置为有效负载的状态

Firebase 重新匹配:注册时将状态设置为有效负载的状态,firebase,react-native,redux,react-redux,rematch,Firebase,React Native,Redux,React Redux,Rematch,如何将状态设置为有效负载的状态?我希望我的全局状态具有最近的更改,而不是有效负载。有人能解释一下为什么会发生这种事吗?我是否需要创建另一个减速器/效果来设置状态?我想在应用程序中将此状态设置为全局状态。我将不胜感激 我正在使用以下工具: FirebaseFirestore 重赛重赛 本地反应 这是 这是我的代码: Index.js import { init } from '@rematch/core' import user from './user' const models = {

如何将状态设置为有效负载的状态?我希望我的全局状态具有最近的更改而不是有效负载。有人能解释一下为什么会发生这种事吗?我是否需要创建另一个减速器/效果来设置状态?我想在应用程序中将此状态设置为全局状态。我将不胜感激

我正在使用以下工具:

  • FirebaseFirestore
  • 重赛重赛
  • 本地反应
这是

这是我的代码

Index.js

import { init } from '@rematch/core'
import user from './user'

const models = {
    user,
}

const store = init({
    models,
})

export default { getState, dispatch } = store
import firebase from 'firebase';
import db from '../config/firebase'

const user = {
    state: {},
    reducers: {
        login(userData) {
            return userData
        },
        email(state, email) {
            return { ...state, email }
        },
        password(state, password) {
            return { ...state, password }
        },
        username(state, username) {
            return { ...state, username }
        },
        fullname(state, fullname) {
            return { ...state, fullname }
        },
    },
    effects: () => ({
        async signup() {
            const { email, password, username, fullname } = getState().user
            const response = await firebase.auth().createUserWithEmailAndPassword(email, password)
            if (response.user.uid) {
                const userData = {
                    uid: response.user.uid,
                    email: email,
                    username: username,
                    fullname: fullname,
                    bio: 'test',
                    gender: 'teste',
                    phoneNum: 'teste',
                    profilePic: 'te',
                    status: 'teste',
                }

                db.collection('users').doc(response.user.uid).set(userData)
                alert(userData.uid)
                return dispatch.user.login(userData)
            }
        }
    })
}

export default user
import * as React from 'react';
import {
    TextInput,
    Text,
    KeyboardAvoidingView,
    SafeAreaView,
    TouchableOpacity,
    Alert,
}
    from 'react-native';
import styles from '../styles'
import { connect } from 'react-redux';
import '@expo/vector-icons';
import 'redux';

class Signup extends React.Component {

    onPress = () => {
        this.props.SignUp()
        this.props.navigation.navigate('Home')
    }

    render() {
        const { routeName } = this.props.navigation.state
        return (
            <SafeAreaView style={styles.container}>
                <KeyboardAvoidingView behavior='position'>
                    <Text style={styles.mainText}>
                        EMAIL
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        editable={routeName === 'Signup' ? true : false}
                        value={this.props.user.email}
                        onChangeText={input => this.props.setEmail(input)}
                    />
                    <Text style={styles.mainText}>
                        PASSWORD
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        editable={routeName === 'Signup' ? true : false}
                        value={this.props.user.password}
                        onChangeText={input => this.props.setPassword(input)}
                        secureTextEntry={true}
                    />
                    <Text style={styles.mainText}>
                        USERNAME
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        value={this.props.user.username}
                        onChangeText={input => this.props.setUserName(input)}
                    />
                    <Text style={styles.mainText}>
                        FULL NAME
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        value={this.props.user.fullname}
                        onChangeText={input => this.props.setFullName(input)}
                    />
                    <TouchableOpacity
                        style={styles.buttonLighGray}
                        onPress={() => this.onPress()}>
                        <Text style={styles.buttonDarkText}>
                            Accept & Sign Up
                        </Text>
                    </TouchableOpacity>
                </KeyboardAvoidingView>
            </SafeAreaView>
        );
    }
}

const mapState = (state) => ({
    user: state.user,
})

const mapDispatch = (dispatch) => ({
    setEmail: mail => dispatch.user.email(mail),
    setPassword: pass => dispatch.user.password(pass),
    setUserName: usern => dispatch.user.username(usern),
    setFullName: fulln => dispatch.user.fullname(fulln),
    SignUp: () => dispatch.user.signup(),
})

export default connect(mapState, mapDispatch)(Signup)
import * as React from 'react';
import {
    View,
    TextInput,
    Alert,
    Text,
    KeyboardAvoidingView,
    SafeAreaView,
    TouchableOpacity,
}
    from 'react-native';
import styles from '../styles'
import { connect } from 'react-redux';
import { Image } from 'react-native-elements';
import '@expo/vector-icons';
import 'redux';
import firebase from 'firebase' 

class Screen extends React.Component {
    render() {
        return (
            <SafeAreaView style={styles.container}>
                    <Text> Full Name: {this.props.user.fullName}</Text>
                    <Text> Email: {this.props.user.email}</Text>
                    <Text> username: {this.props.user.username}</Text>
                    <Text> bio: {this.props.user.bio}</Text>
                    <Text> gender: {this.props.user.gender}</Text>
                    <Text> phoneNum: {this.props.user.phoneNum}</Text>
                    <Text> profilePic: {this.props.user.profilePic}</Text>
            </SafeAreaView>
        );
    }
}

const mapState = (state) => ({
    user: state.user,
})

const mapDispatch = (dispatch) => ({
    setEmail: mail => dispatch.user.email(mail),
    setPassword: pass => dispatch.user.password(pass),
    setUserName: usern => dispatch.user.username(usern),
    setFullName: fulln => dispatch.user.fullname(fulln),
})

export default connect(mapState, mapDispatch)(Screen)
Model.js(User.js)

SignUp.js

import { init } from '@rematch/core'
import user from './user'

const models = {
    user,
}

const store = init({
    models,
})

export default { getState, dispatch } = store
import firebase from 'firebase';
import db from '../config/firebase'

const user = {
    state: {},
    reducers: {
        login(userData) {
            return userData
        },
        email(state, email) {
            return { ...state, email }
        },
        password(state, password) {
            return { ...state, password }
        },
        username(state, username) {
            return { ...state, username }
        },
        fullname(state, fullname) {
            return { ...state, fullname }
        },
    },
    effects: () => ({
        async signup() {
            const { email, password, username, fullname } = getState().user
            const response = await firebase.auth().createUserWithEmailAndPassword(email, password)
            if (response.user.uid) {
                const userData = {
                    uid: response.user.uid,
                    email: email,
                    username: username,
                    fullname: fullname,
                    bio: 'test',
                    gender: 'teste',
                    phoneNum: 'teste',
                    profilePic: 'te',
                    status: 'teste',
                }

                db.collection('users').doc(response.user.uid).set(userData)
                alert(userData.uid)
                return dispatch.user.login(userData)
            }
        }
    })
}

export default user
import * as React from 'react';
import {
    TextInput,
    Text,
    KeyboardAvoidingView,
    SafeAreaView,
    TouchableOpacity,
    Alert,
}
    from 'react-native';
import styles from '../styles'
import { connect } from 'react-redux';
import '@expo/vector-icons';
import 'redux';

class Signup extends React.Component {

    onPress = () => {
        this.props.SignUp()
        this.props.navigation.navigate('Home')
    }

    render() {
        const { routeName } = this.props.navigation.state
        return (
            <SafeAreaView style={styles.container}>
                <KeyboardAvoidingView behavior='position'>
                    <Text style={styles.mainText}>
                        EMAIL
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        editable={routeName === 'Signup' ? true : false}
                        value={this.props.user.email}
                        onChangeText={input => this.props.setEmail(input)}
                    />
                    <Text style={styles.mainText}>
                        PASSWORD
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        editable={routeName === 'Signup' ? true : false}
                        value={this.props.user.password}
                        onChangeText={input => this.props.setPassword(input)}
                        secureTextEntry={true}
                    />
                    <Text style={styles.mainText}>
                        USERNAME
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        value={this.props.user.username}
                        onChangeText={input => this.props.setUserName(input)}
                    />
                    <Text style={styles.mainText}>
                        FULL NAME
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        value={this.props.user.fullname}
                        onChangeText={input => this.props.setFullName(input)}
                    />
                    <TouchableOpacity
                        style={styles.buttonLighGray}
                        onPress={() => this.onPress()}>
                        <Text style={styles.buttonDarkText}>
                            Accept & Sign Up
                        </Text>
                    </TouchableOpacity>
                </KeyboardAvoidingView>
            </SafeAreaView>
        );
    }
}

const mapState = (state) => ({
    user: state.user,
})

const mapDispatch = (dispatch) => ({
    setEmail: mail => dispatch.user.email(mail),
    setPassword: pass => dispatch.user.password(pass),
    setUserName: usern => dispatch.user.username(usern),
    setFullName: fulln => dispatch.user.fullname(fulln),
    SignUp: () => dispatch.user.signup(),
})

export default connect(mapState, mapDispatch)(Signup)
import * as React from 'react';
import {
    View,
    TextInput,
    Alert,
    Text,
    KeyboardAvoidingView,
    SafeAreaView,
    TouchableOpacity,
}
    from 'react-native';
import styles from '../styles'
import { connect } from 'react-redux';
import { Image } from 'react-native-elements';
import '@expo/vector-icons';
import 'redux';
import firebase from 'firebase' 

class Screen extends React.Component {
    render() {
        return (
            <SafeAreaView style={styles.container}>
                    <Text> Full Name: {this.props.user.fullName}</Text>
                    <Text> Email: {this.props.user.email}</Text>
                    <Text> username: {this.props.user.username}</Text>
                    <Text> bio: {this.props.user.bio}</Text>
                    <Text> gender: {this.props.user.gender}</Text>
                    <Text> phoneNum: {this.props.user.phoneNum}</Text>
                    <Text> profilePic: {this.props.user.profilePic}</Text>
            </SafeAreaView>
        );
    }
}

const mapState = (state) => ({
    user: state.user,
})

const mapDispatch = (dispatch) => ({
    setEmail: mail => dispatch.user.email(mail),
    setPassword: pass => dispatch.user.password(pass),
    setUserName: usern => dispatch.user.username(usern),
    setFullName: fulln => dispatch.user.fullname(fulln),
})

export default connect(mapState, mapDispatch)(Screen)
import*as React from'React';
进口{
文本输入,
文本
键盘避免了gView,
安全区域视图,
可触摸不透明度,
警觉的
}
从“反应本机”;
从“../styles”导入样式
从'react redux'导入{connect};
导入“@expo/vector图标”;
输入“redux”;
类注册扩展了React.Component{
onPress=()=>{
this.props.SignUp()
this.props.navigation.navigate('Home'))
}
render(){
const{routeName}=this.props.navigation.state
返回(
电子邮件
this.props.setEmail(输入)}
/>
暗语
this.props.setPassword(输入)}
secureTextEntry={true}
/>
用户名
this.props.setUserName(输入)}
/>
全名
this.props.setFullName(输入)}
/>
this.onPress()}>
接受并注册
);
}
}
常量映射状态=(状态)=>({
用户:state.user,
})
常量映射分派=(分派)=>({
setEmail:mail=>dispatch.user.email(邮件),
setPassword:pass=>dispatch.user.password(pass),
setUserName:usern=>dispatch.user.username(usern),
setFullName:fulln=>dispatch.user.fullname(fulln),
注册:()=>dispatch.user.SignUp(),
})
导出默认连接(mapState、mapDispatch)(注册)
Screen.js

import { init } from '@rematch/core'
import user from './user'

const models = {
    user,
}

const store = init({
    models,
})

export default { getState, dispatch } = store
import firebase from 'firebase';
import db from '../config/firebase'

const user = {
    state: {},
    reducers: {
        login(userData) {
            return userData
        },
        email(state, email) {
            return { ...state, email }
        },
        password(state, password) {
            return { ...state, password }
        },
        username(state, username) {
            return { ...state, username }
        },
        fullname(state, fullname) {
            return { ...state, fullname }
        },
    },
    effects: () => ({
        async signup() {
            const { email, password, username, fullname } = getState().user
            const response = await firebase.auth().createUserWithEmailAndPassword(email, password)
            if (response.user.uid) {
                const userData = {
                    uid: response.user.uid,
                    email: email,
                    username: username,
                    fullname: fullname,
                    bio: 'test',
                    gender: 'teste',
                    phoneNum: 'teste',
                    profilePic: 'te',
                    status: 'teste',
                }

                db.collection('users').doc(response.user.uid).set(userData)
                alert(userData.uid)
                return dispatch.user.login(userData)
            }
        }
    })
}

export default user
import * as React from 'react';
import {
    TextInput,
    Text,
    KeyboardAvoidingView,
    SafeAreaView,
    TouchableOpacity,
    Alert,
}
    from 'react-native';
import styles from '../styles'
import { connect } from 'react-redux';
import '@expo/vector-icons';
import 'redux';

class Signup extends React.Component {

    onPress = () => {
        this.props.SignUp()
        this.props.navigation.navigate('Home')
    }

    render() {
        const { routeName } = this.props.navigation.state
        return (
            <SafeAreaView style={styles.container}>
                <KeyboardAvoidingView behavior='position'>
                    <Text style={styles.mainText}>
                        EMAIL
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        editable={routeName === 'Signup' ? true : false}
                        value={this.props.user.email}
                        onChangeText={input => this.props.setEmail(input)}
                    />
                    <Text style={styles.mainText}>
                        PASSWORD
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        editable={routeName === 'Signup' ? true : false}
                        value={this.props.user.password}
                        onChangeText={input => this.props.setPassword(input)}
                        secureTextEntry={true}
                    />
                    <Text style={styles.mainText}>
                        USERNAME
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        value={this.props.user.username}
                        onChangeText={input => this.props.setUserName(input)}
                    />
                    <Text style={styles.mainText}>
                        FULL NAME
                    </Text>
                    <TextInput
                        style={styles.inputText}
                        value={this.props.user.fullname}
                        onChangeText={input => this.props.setFullName(input)}
                    />
                    <TouchableOpacity
                        style={styles.buttonLighGray}
                        onPress={() => this.onPress()}>
                        <Text style={styles.buttonDarkText}>
                            Accept & Sign Up
                        </Text>
                    </TouchableOpacity>
                </KeyboardAvoidingView>
            </SafeAreaView>
        );
    }
}

const mapState = (state) => ({
    user: state.user,
})

const mapDispatch = (dispatch) => ({
    setEmail: mail => dispatch.user.email(mail),
    setPassword: pass => dispatch.user.password(pass),
    setUserName: usern => dispatch.user.username(usern),
    setFullName: fulln => dispatch.user.fullname(fulln),
    SignUp: () => dispatch.user.signup(),
})

export default connect(mapState, mapDispatch)(Signup)
import * as React from 'react';
import {
    View,
    TextInput,
    Alert,
    Text,
    KeyboardAvoidingView,
    SafeAreaView,
    TouchableOpacity,
}
    from 'react-native';
import styles from '../styles'
import { connect } from 'react-redux';
import { Image } from 'react-native-elements';
import '@expo/vector-icons';
import 'redux';
import firebase from 'firebase' 

class Screen extends React.Component {
    render() {
        return (
            <SafeAreaView style={styles.container}>
                    <Text> Full Name: {this.props.user.fullName}</Text>
                    <Text> Email: {this.props.user.email}</Text>
                    <Text> username: {this.props.user.username}</Text>
                    <Text> bio: {this.props.user.bio}</Text>
                    <Text> gender: {this.props.user.gender}</Text>
                    <Text> phoneNum: {this.props.user.phoneNum}</Text>
                    <Text> profilePic: {this.props.user.profilePic}</Text>
            </SafeAreaView>
        );
    }
}

const mapState = (state) => ({
    user: state.user,
})

const mapDispatch = (dispatch) => ({
    setEmail: mail => dispatch.user.email(mail),
    setPassword: pass => dispatch.user.password(pass),
    setUserName: usern => dispatch.user.username(usern),
    setFullName: fulln => dispatch.user.fullname(fulln),
})

export default connect(mapState, mapDispatch)(Screen)
import*as React from'React';
进口{
看法
文本输入,
警觉的
文本
键盘避免了gView,
安全区域视图,
可触摸不透明度,
}
从“反应本机”;
从“../styles”导入样式
从'react redux'导入{connect};
从“react native elements”导入{Image};
导入“@expo/vector图标”;
输入“redux”;
从“firebase”导入firebase
类屏幕扩展了React.Component{
render(){
返回(
全名:{this.props.user.fullName}
电子邮件:{this.props.user.Email}
用户名:{this.props.user.username}
bio:{this.props.user.bio}
性别:{this.props.user.gender}
phoneNum:{this.props.user.phoneNum}
profilePic:{this.props.user.profilePic}
);
}
}
常量映射状态=(状态)=>({
用户:state.user,
})
常量映射分派=(分派)=>({
setEmail:mail=>dispatch.user.email(邮件),
setPassword:pass=>dispatch.user.password(pass),
setUserName:usern=>dispatch.user.username(usern),
setFullName:fulln=>dispatch.user.fullname(fulln),
})
导出默认连接(mapState、mapDispatch)(屏幕)

问题是您在登录窗口中再次返回当前的状态。(您声明的是用户数据)

登录(状态、负载){
返回{
状态
…有效载荷
}
//这将采用全局状态并覆盖有效负载中的所有内容(合并两个对象)
},
否则,您可以只执行
返回有效负载
,但这可能会在将来覆盖其他存储值