小编Jor*_*sta的帖子

"无状态"组件中的上下文?

我在组件中有以下代码,我想要一个无状态组件来访问这部分代码:

主要成分:

function createApp(store, communityIds) {
const App = React.createClass({

    childContextTypes: {
        localizedString: React.PropTypes.func,
    },

    getChildContext: function() {
        return {
            localizedString: function(key, fallback) {
                return getKey(key, fallback);
            },
        };
    },

    render: function() {
        return (
            <Provider store={store}>
                <Client communityIds={communityIds}/>
            </Provider>
        );
    },
});

return <App/>;
}
Run Code Online (Sandbox Code Playgroud)

无状态:

export default () => (dispatch, getState) => {
    const state = getState();

    const token = state.user.get('token');

    if (!token) {
        throw new Error('test'); // this.context.localizedString does not work
    }
}
Run Code Online (Sandbox Code Playgroud)

reactjs

36
推荐指数
3
解决办法
2万
查看次数

标签 统计

reactjs ×1