简体   繁体   English

在 React Native 组件中获取 Redux 存储数据

[英]Get Redux store data in React Native component

I'm getting started in Redux and React Native and I'm struggling a lot trying to fetch stored data in a component, It's anything special, just a button to mutate the store and another to print it as Im still a newborn in this我开始使用 Redux 和 React Native,我在尝试获取组件中存储的数据时遇到了很多困难,这很特别,只是一个按钮来改变商店,另一个按钮来打印它,因为我还是个新生儿

// TestComponent.js

<Button onPress={ () => { this.props.todoAdd("cafee") } }>
    <Text>Covfefe</Text>
</Button>

<Button onPress={ () => { console.log(this.state) } }>
    <Text>Todo alc</Text>
</Button>

const mapStateToProps = (state, props) => {
  return {
    todos: state,
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    todoAdd: (name) => dispatch({type: 'TODO_ADD', todo: { id: '0', name, completed: false }}),
  };
};

export default connect(mapStateToProps, mapDispatchToProps)(TestComponent)

The add button works, I registered a suscription in App.js so I can see every update in the store, Im glad at least one thing does works but Im so confused, I want to fetch the stored data but I cannot make it work!添加按钮有效,我在 App.js 中注册了一个订阅,因此我可以看到商店中的每一个更新,我很高兴至少有一件事情有效但我很困惑,我想获取存储的数据但我无法让它工作!

I read that mapStateToProps actually listens for changes and triggers, updating the props of the component with the data you wrote that you component need, I wrote props.todos = state so the entire state should be in this.props.todos, doesn't it?我读到 mapStateToProps 实际上侦听更改和触发器,使用您编写的组件需要的数据更新组件的道具,我写了 props.todos = state 所以整个 state 应该在 this.props.todos 中,不它? I dont know but when I print the component props I get an empty object.我不知道,但是当我打印组件道具时,我得到一个空的 object。

There are the other configuration files from my store:我的商店还有其他配置文件:

// store/index.js

import {createStore} from 'redux';
import Reducers from './reducers'

export default configureStore = () => {
    let store = createStore(Reducers, [])
    return store
}
// store/reducers.js

export default function reducer(state, action) {
    switch(action.type) {
      case 'TODO_ADD' : {
        return applyAddTodo(state, action);
      }
      case 'TODO_TOGGLE' : {
        return applyToggleTodo(state, action);
      }
      default : return state;
    }
  }
  function applyAddTodo(state, action) {
    return state.concat(action.todo);
  }
  function applyToggleTodo(state, action) {
    return state.map(todo =>
      todo.id === action.todo.id
        ? Object.assign({}, todo, { completed: !todo.completed })
        : todo
    );
}
// App.js

import {Provider} from 'react-redux';
import configureStore from './store/index'

let store = configureStore()
const unsubscribe = store.subscribe(() => {
  console.log('store update, current state:');
  console.log(store.getState());
});

const MainNavigator = createStackNavigator({
  Home: {screen: TestComponent},
});

const Root = createAppContainer(MainNavigator);

export default class App extends Component {
  render() {
    return (
      <Provider store={store}>
        <Root />
      </Provider>
    );
  }
}

I just want to know how can I fetch the data to print it, I just started learning redux so any tip is greatly welcomed, anyways thank you for reading me.我只是想知道如何获取数据以打印它,我刚开始学习 redux 所以非常欢迎任何提示,无论如何感谢您阅读我。

Try console.log(this.props.todos) Since you are mapping your whole redux store state to the todos object you should find it at this.props.todos试试console.log(this.props.todos)因为你将整个 redux 存储区 state 映射到todos object,你应该在this.props.todos中找到它。

Okay so I found what was wrong with my code, when I try to print my store I was doing console.log(this.state) convinced that mapStateToProps modifies the actual state of the component but it doesnt, it passes the state to the props of the component (as the function name indicates...):c好的,所以我发现我的代码有什么问题,当我尝试打印我的商店时,我正在执行console.log(this.state)确信 mapStateToProps 修改了组件的实际 state 但它没有,它将 state 传递给道具组件的(如 function 名称所示...):c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM