首页 > 解决方案 > Redux get store.getState() 未定义

问题描述

我正在从反应中的常规有状态组件迁移到减少。当我尝试console.log store.getState() 在返回函数中运行时,它显示为未定义状态。我不确定我在这里做错了什么。任何帮助将非常感激!

const store = createStore(reducers, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
class Products extends React.Component {

    constructor (){
      super();
      this.state = {
          productImages: [],
          fetching: false
      }
    }

    componentDidMount(){
        this.setState({
            fetching: true,
        });

        fetch('https://omenu.com/test/jsonimages')
            .then(response => response.json())
            .then(products => products.map(product => product.image))
            .then(products => store.dispatch(addLink(products)))
            .catch(err => console.log(err))

    }

    render(){
       return(
          <Provider store={store}>
              {console.log(store,"Store")} //having a problem here ----- It showing undefined
              <ScrollView horizontal={true}>
                  <ActivityIndicator size="large" style={styles.spinner} animating={true}/>
                     {this.state.productImages.map((uri,i)=>(
                        <Image style={styles.thumb} key={i} source={{uri}}/>
                     ))}
              </ScrollView>
          </Provider>
       )
    }
}

标签: reactjsreact-nativeredux

解决方案


推荐阅读