我无法从另一个组件中读取我的状态(Redux)?

huangapple go评论63阅读模式
英文:

I cannot read my state from another component (Redux)?

问题

I'm having an issue about reading my state from another component. I use Redux Toolkit to store my state.

colorPick.js :

import { createSlice } from '@reduxjs/toolkit';

export const colorizerSlice = createSlice({
  name: 'colorizer',
  initialState: {
    color: { nav: '#cecece',
             bg: '#ebebeb' }
  },
  reducers: {
    yellow: state => {
      state.color = { nav: '#e1ce00',
                      bg: '#ffed27' }
    },
    red: state => {
      state.color = { nav: '#e11300',
                      bg: '#ff3d27' }
    },
    green: state => {
      state.color = { nav: '#00e104',
                      bg: '#4bff27' }
    },
    pink: state => {
      state.color = { nav: '#e100ce',
                      bg: '#ff27fb' }
    },
    orange: state => {
      state.color = { nav: '#e18b00',
                      bg: '#ff8527' }
    },
    // incrementByAmount: (state, action) => {
    //   state.value += action.payload
    // }
  }
})

// Action creators are generated for each case reducer function
export const { yellow,
               red,
               green,
               pink,
               orange, } = colorizerSlice.actions

export default colorizerSlice.reducer

store.js :

import { configureStore } from '@reduxjs/toolkit'
import colorizerReducer from './reducers/colorPick';

export default configureStore({
  reducer: {
    colorizer: colorizerReducer,
  }
})

App.js :

import { StatusBar } from 'expo-status-bar';
import { useState, useEffect } from 'react';
import { Button, StyleSheet, Text, TextInput, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { Provider } from 'react-redux';

import Notes from './screens/Notes';
import ViewNote from './screens/ViewNote';
import EditNote from './screens/EditNote';
import AddNote from './screens/AddNote';
import store from './redux/store';

export default function App() {
  
  const Stack = createNativeStackNavigator();

  return (
    <Provider store={store}>
    <NavigationContainer>
      <Stack.Navigator screenOptions={defaultOptions}>
        <Stack.Screen name='Notes' component={Notes} options={notesOptions} />
        <Stack.Screen name='ViewNote' component={ViewNote} options={viewNotesOptions} />
        <Stack.Screen name='EditNote' component={EditNote} options={editNotesOptions} />
        <Stack.Screen name='AddNote' component={AddNote} options={addNotesOptions} />
      </Stack.Navigator>
    </NavigationContainer>
    </Provider>
  );
}

ColorPalette.js : (I can read my state in this component)

import { StyleSheet, Text, View, Pressable } from 'react-native'
import React from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { green, red, yellow, orange, pink } from '../redux/reducers/colorPick';

const ColorPalette = () => {
  const { color } = useSelector((state) => state.colorizer);
  const dispatch = useDispatch();
  console.log(color.nav);
  return (
    <View style={styles.paletteContainer}>
      <Pressable style={[styles.color, {backgroundColor: '#ffed27'}]} android_ripple={{color: '#d9d9d9'}} onPress={() => dispatch(yellow())} />
      <Pressable style={[styles.color, {backgroundColor: '#ff3d27'}]} android_ripple={{color: '#d9d9d9'}} onPress={() => dispatch(red())} />
      <Pressable style={[styles.color, {backgroundColor: '#4bff27'}]} android_ripple={{color: '#d9d9d9'}} onPress={() => dispatch(green())} />
      <Pressable style={[styles.color, {backgroundColor: '#ff27fb'}]} android_ripple={{color: '#d9d9d9'}} onPress={() => dispatch(pink())} />
      <Pressable style={[styles.color, {backgroundColor: '#ff8527'}]} android_ripple={{color: '#d9d9d9'}} onPress={() => dispatch(orange())} />
      <View style={{backgroundColor: color.nav, width:20}}></View>
      <View style={{backgroundColor: color.bg, width:20}}></View>
    </View>
  )
}

NoteTile.js : (I cannot read in this component)

import { StyleSheet, Text, View, Pressable } from 'react-native'
import React from 'react'
import { useSelector } from 'react-redux';

const NoteTile = ({title, det, pressFunc}) => {
    const { color } = useSelector((state) => state.colorizer);
    // const color = useSelector((state) => state.colorizer.color);
  return (
    <View style={styles.tileContainer} >
        <Pressable  android_ripple={{color: '#cccccc'}} 
                    style={styles.button}
                    onPress={pressFunc} >
            <View style={[styles.innerContainer]} >
                <Text style={styles.title} >{title}</Text>
                <Text style={styles.details} >{det}</Text>
            </View>
        </Pressable>
    </View>
  )
}

export default NoteTile

const styles = StyleSheet.create({
    tileContainer: {
        flex: 1,
        margin: 10,
        height: 100,
        maxHeight: 400,
        borderRadius: 30,
        elevation: 5,
        overflow: 'hidden',
        // backgroundColor: '#ffffff'
        backgroundColor: color.bg,
    }})

The error that I got is 'Can't find variable: color';

I wrapped my code with Provider. And I use NoteTile.js in Notes Screen which is within the Provider.

英文:

I'm having an issue about reading my state from another component. I use Redux Toolkit to store my state.

colorPick.js :

import { createSlice } from &#39;@reduxjs/toolkit&#39;
export const colorizerSlice = createSlice({
name: &#39;colorizer&#39;,
initialState: {
color: { nav: &#39;#cecece&#39;,
bg: &#39;#ebebeb&#39; }
},
reducers: {
yellow: state =&gt; {
state.color = { nav: &#39;#e1ce00&#39;,
bg: &#39;#ffed27&#39; }
},
red: state =&gt; {
state.color = { nav: &#39;#e11300&#39;,
bg: &#39;#ff3d27&#39; }
},
green: state =&gt; {
state.color = { nav: &#39;#00e104&#39;,
bg: &#39;#4bff27&#39; }
},
pink: state =&gt; {
state.color = { nav: &#39;#e100ce&#39;,
bg: &#39;#ff27fb&#39; }
},
orange: state =&gt; {
state.color = { nav: &#39;#e18b00&#39;,
bg: &#39;#ff8527&#39; }
},
// incrementByAmount: (state, action) =&gt; {
//   state.value += action.payload
// }
}
})
// Action creators are generated for each case reducer function
export const { yellow,
red,
green,
pink,
orange, } = colorizerSlice.actions
export default colorizerSlice.reducer

store.js :

import { configureStore } from &#39;@reduxjs/toolkit&#39;
import colorizerReducer from &#39;./reducers/colorPick&#39;
export default configureStore({
reducer: {
colorizer: colorizerReducer,
}
})

App.js :

import { StatusBar } from &#39;expo-status-bar&#39;;
import { useState, useEffect } from &#39;react&#39;;
import { Button, StyleSheet, Text, TextInput, View } from &#39;react-native&#39;;
import { NavigationContainer } from &#39;@react-navigation/native&#39;;
import { createNativeStackNavigator } from &#39;@react-navigation/native-stack&#39;;
import { Provider } from &#39;react-redux&#39;;
import Notes from &#39;./screens/Notes&#39;;
import ViewNote from &#39;./screens/ViewNote&#39;;
import EditNote from &#39;./screens/EditNote&#39;;
import AddNote from &#39;./screens/AddNote&#39;;
import store from &#39;./redux/store&#39;;
export default function App() {
const Stack = createNativeStackNavigator();
return (
&lt;Provider store={store}&gt;
&lt;NavigationContainer&gt;
&lt;Stack.Navigator screenOptions={defaultOptions}&gt;
&lt;Stack.Screen name=&#39;Notes&#39; component={Notes} options={notesOptions} /&gt;
&lt;Stack.Screen name=&#39;ViewNote&#39; component={ViewNote} options={viewNotesOptions} /&gt;
&lt;Stack.Screen name=&#39;EditNote&#39; component={EditNote} options={editNotesOptions} /&gt;
&lt;Stack.Screen name=&#39;AddNote&#39; component={AddNote} options={addNotesOptions} /&gt;
&lt;/Stack.Navigator&gt;
&lt;/NavigationContainer&gt;
&lt;/Provider&gt;
);
}

ColorPalette.js : (I can read my state in this component)

import { StyleSheet, Text, View, Pressable } from &#39;react-native&#39;
import React from &#39;react&#39;
import { useDispatch, useSelector } from &#39;react-redux&#39;
import { green, red, yellow, orange, pink } from &#39;../redux/reducers/colorPick&#39;
const ColorPalette = () =&gt; {
const { color } = useSelector((state) =&gt; state.colorizer);
const dispatch = useDispatch();
console.log(color.nav);
return (
&lt;View style={styles.paletteContainer}&gt;
&lt;Pressable style={[styles.color, {backgroundColor: &#39;#ffed27&#39;}]} android_ripple={{color: &#39;#d9d9d9&#39;}} onPress={() =&gt; dispatch(yellow())} /&gt;
&lt;Pressable style={[styles.color, {backgroundColor: &#39;#ff3d27&#39;}]} android_ripple={{color: &#39;#d9d9d9&#39;}} onPress={() =&gt; dispatch(red())} /&gt;
&lt;Pressable style={[styles.color, {backgroundColor: &#39;#4bff27&#39;}]} android_ripple={{color: &#39;#d9d9d9&#39;}} onPress={() =&gt; dispatch(green())} /&gt;
&lt;Pressable style={[styles.color, {backgroundColor: &#39;#ff27fb&#39;}]} android_ripple={{color: &#39;#d9d9d9&#39;}} onPress={() =&gt; dispatch(pink())} /&gt;
&lt;Pressable style={[styles.color, {backgroundColor: &#39;#ff8527&#39;}]} android_ripple={{color: &#39;#d9d9d9&#39;}} onPress={() =&gt; dispatch(orange())} /&gt;
&lt;View style={{backgroundColor: color.nav, width:20}}&gt;&lt;/View&gt;
&lt;View style={{backgroundColor: color.bg, width:20}}&gt;&lt;/View&gt;
&lt;/View&gt;
)
}

NoteTile.js : (I cannot read in this component)

import { StyleSheet, Text, View, Pressable } from &#39;react-native&#39;
import React from &#39;react&#39;
import { useSelector } from &#39;react-redux&#39;
const NoteTile = ({title, det, pressFunc}) =&gt; {
const { color } = useSelector((state) =&gt; state.colorizer);
// const color = useSelector((state) =&gt; state.colorizer.color);
return (
&lt;View style={styles.tileContainer} &gt;
&lt;Pressable  android_ripple={{color: &#39;#cccccc&#39;}} 
style={styles.button}
onPress={pressFunc} &gt;
&lt;View style={[styles.innerContainer]} &gt;
&lt;Text style={styles.title} &gt;{title}&lt;/Text&gt;
&lt;Text style={styles.details} &gt;{det}&lt;/Text&gt;
&lt;/View&gt;
&lt;/Pressable&gt;
&lt;/View&gt;
)
}
export default NoteTile
const styles = StyleSheet.create({
tileContainer: {
flex: 1,
margin: 10,
height: 100,
maxHeight: 400,
borderRadius: 30,
elevation: 5,
overflow: &#39;hidden&#39;,
// backgroundColor: &#39;#ffffff&#39;
backgroundColor: color.bg,
}})

The error that I got is 'Can't find variable: color'

I wrapped my code with Provider.
And I use NoteTile.js in Notes Screen which is within the Provider.

答案1

得分: 0

尝试以这种方式使用它:

<View style={[styles.tileContainer, { backgroundColor: color.bg }]}>
英文:

try using it this way

&lt;View style={[styles.tileContainer,{ backgroundColor:color.bg }]} &gt;

huangapple
  • 本文由 发表于 2023年2月18日 22:23:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定