简体   繁体   English

如何基于Rx.ReplaySubject模型在React中设置初始状态

[英]How to set the initial state in React based on a Rx.ReplaySubject model

I am trying to figure out how to make react both isomporpic and Rx.js driven. 我试图找出如何使isomporpic和Rx.js驱动的反应。

My use case (at this point) is getting the locale into state of my components so that my i18n can use it for string selection. 我的用例(此时)是使语言环境进入组件状态,以便我的i18n可以将其用于字符串选择。

Presuming I have a simple model for locale 假设我有一个简单的语言环境模型

import Rx from 'rxjs';

const localeModel = new Rx.ReplaySubject('en');

export default localeModel;

how can I set the initial state of the compoent 如何设置组件的初始状态

class MyHeader extends Component {
    constructor () {
        super();
        this.state = {locale: //???? 
         };
    }
}

I can of course use componentDidMount, subscribe, and setState to eventually get the right locale, but I want to have it have the right value on the server. 我当然可以使用componentDidMount,subscribe和setState来最终获得正确的语言环境,但是我想让它在服务器上具有正确的值。

(in reality I want to do a lot more than just set the locale - I want to pull strings into my page based on the known ID of the component and the locale). (实际上,我要做的不仅仅是设置语言环境-我还想根据组件的已知ID和语言环境将字符串拉入页面中)。

So, given that Rx.js's basic philosophy is streaming, how do I get the current value of the observable synchronously in the constructor and stay in the canon of using Rx in a streaming context? 因此,鉴于Rx.js的基本原理是流式传输,如何在构造函数中同步获取可观察对象的当前值,并保持流式传输上下文中使用Rx的规范?

you can use the import like 你可以像这样使用import

import i18n from 'localModel'

class MyHeader extends Component {
    constructor () {
        super();
        this.state = {locale: i18n}
         };
    }
}

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

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