首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在react-native中将更改的状态发送到组件?

如何在react-native中将更改的状态发送到组件?
EN

Stack Overflow用户
提问于 2018-06-03 16:50:57
回答 2查看 80关注 0票数 0

我是react & react-native的初学者。我正在使用react 16,react-thunk,react-redux。

我正在尝试从firestore中获取我已经创建的类别。首先,我使用connect()调用action,然后,我使用thunk输入action,也从firestore获取数据。最后,我在reducer中返回了新状态。当然,我不知道redux过程,所以请给我一些提示。

这是我的代码。谢谢。

CategoryImageList.js (组件)

代码语言:javascript
复制
...

class CategoryImageList extends Component {
  componentWillMount() {
    this.props.getCategory();
  }

  renderImages() {
      return this.state.categories.map(category =>
          <CategoryImageCard key={category.imgName} category={category}/>
      );
  }

  render() {
    return (
        <ScrollView>
            {/*{this.renderImages()}*/}
        </ScrollView>
    );
  }
}

export default connect(null, {getCategory})(CategoryImageList);

动作( category.js )

代码语言:javascript
复制
...
export const getCategory = () => {
  return (dispatch) => { //using redux-thunk here... do check it out
    getCategories()
        .then(querySnapshot => {
            const test = [];
            querySnapshot.forEach((doc) => {
                test.push(
                {
                    imgName : doc.data()['imgName'],
                    name : doc.data()['name']
                });
            });

            dispatch({ type: GET_CATEGORY, payload: test} );
        });
  };
};

CategoryReducers.js (reducer)

代码语言:javascript
复制
...

const categoryInitialState = {
  name: [],
  imgName: []
}

export const CategoryReducer = (state = categoryInitialState, action) => {
  switch (action.type) {
    case GET_CATEGORY:
        console.log(action);
        return { ...state, categoryImg: {
            name: action.payload.name,
            imgName: action.payload.imgName
        }};

    default:
        return state;
    }
}

App.js

代码语言:javascript
复制
...
type Props = {};
export default class App extends Component<Props> {
    render() {
        const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));
        return (
        <Provider store={store}>
            <View style={{flex:1}}>
                <Header headerText={'FoodUp'}/>
                <CategoryImageList />
            </View>
        </Provider>
    );
  }
}

reducers/index.js

代码语言:javascript
复制
import { combineReducers } from 'redux';
import { CategoryReducer } from './CategoryReducer';

export default combineReducers({
    categories: CategoryReducer
});

已更新 Firebase.js

代码语言:javascript
复制
const config = {
  ...
};

firebase.initializeApp(config);
const db = firebase.firestore();
const storage = firebase.storage();
const settings = {timestampsInSnapshots: true};
db.settings(settings);

export const getCategories = () => {
  return db.collection('categories').get();
}

export const getCategoryImg = (categoryName, imgName) => {
  const ref = storage.ref(`category/${categoryName}/${imgName}`);
  return ref.getDownloadURL();
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-04 12:56:12

你必须将mapstateToProps添加到你的连接中,

代码语言:javascript
复制
const mapStateToProps = (state: any) => {
  return {
    name: state.categories.name,
    imageName:state.categories.imageName
  };
}

export default connect(mapStateToProps)(CategoryImageList)

然后,您将能够访问名称和图像名称,如this.props.namethis.props.imageName

编辑:要分派GET_CATEGORY,您可以使用mapDispatchToProps,也可以在组件中执行getCategory并分派,如

代码语言:javascript
复制
import {getCategory} from './category'

 componentWillMount() {
    this.props.getCategory(this.props.dispatch);
  }

并将getCategory函数更改为,

代码语言:javascript
复制
export const getCategory = (dispatch) => {
...
dispatch({ type: GET_CATEGORY, payload: test} );
...
}
票数 1
EN

Stack Overflow用户

发布于 2018-06-03 17:06:33

mapStateToProps将Store state作为参数/参数(由react-redux::connect提供),用于将组件与存储状态的特定部分链接起来。在您的例子中,您可以像这样使用。您还可以在组件中使用name、imgName作为props

代码语言:javascript
复制
const mapStateToProps = ({categories}) => {
    const { name, imgName } = categories;
    return {name, imgName};
};
export default connect(mapStateToProps, {getCategory})(CategoryImageList);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50664729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档