简体   繁体   English

Redux和React-通过@connect使组件道具与状态保持同步

[英]Redux and React - Keeping component props in sync with state via @connect

I have an application set up using React and Redux such that I have a root application component wrapped around a provider (which is passing the store down so that my child components can access state and dispatch) like so: 我有一个使用React和Redux设置的应用程序,这样我就将根应用程序组件包装在提供程序周围(该组件将存储向下传递,以便我的子组件可以访问状态和调度),如下所示:

<Provider store={Store}>
    <RootComponent />
</Provider>

My RootComponent essentially uses a JSON file to render Template components that get passed a key and title ( 我的RootComponent本质上使用JSON文件来呈现通过键和标题传递的Template组件(

<Application children={children} />

Within one of the children, I've hooked up the state of the application to the Component using the @connect decorator and I'm firing off a dispatcher upon construction to update my 'content' prop like so: 在其中一个孩子中,我使用@connect装饰器将应用程序的状态连接到Component,并在构造时解雇了一个调度程序以更新我的“ content”属性,如下所示:

@connect(state => ({content: state.content}))
export default class DynamicText extends Component {

constructor(props) {
    super(props);
    var Content = require(`./jsonContent`);
    props.dispatch(loadContent(Content[props.title].elements));
}

**actions.js**
export function loadContent(content) {
    return {
        type: 'LOAD_CONTENT',
        content
    };
}

**reducers.js**
const initialState = {
route: null
};

export default function routeReducer(state = initialState, action) {
    switch (action.type) {
        case LOAD_CONTENT:
            return Object.assign({}, state, {
                content: action.content
            });
        default:
            return state;
}

The issue I'm having is that when booting up my application, the 'content' prop is undefined - upon page refresh however, it populates with the updated 'content' data from the JSON file. 我遇到的问题是,在启动应用程序时,“内容”属性未定义-但是,在页面刷新时,它会填充来自JSON文件的更新的“内容”数据。 I think the issue may be occurring because initially the state.content referenced in my @connect decorator is undefined, however I was hoping that the immediate dispatch on the constructor would solve this issue. 我认为问题可能是因为最初在我的@connect装饰器中引用的state.content是未定义的,但是我希望对构造函数的立即分派能够解决此问题。

Does anyone have experience or recommendations on how I can get this working without a refresh? 有没有人有经验或关于我如何能不费吹灰之力地提出建议? Is @connect really the best option here or are there alternatives? @connect真的是这里最好的选择吗?还是有其他选择?

You need to hydrate your store before passing it down to Provider providing an initial state to it so that when your app "boots" your store already has the data available. 您需要先对商店进行充水,然后再将其传递给提供初始状态的Provider ,以便在应用程序“启动”时商店已经具有可用的数据。

Using connect is the right way to go after that. 使用connect是正确的方法。 If you're running a server side rendered application you can put your json contents in a property attached to window on first request then empty it after store has been hydrated. 如果您正在运行服务器端渲染的应用程序,则可以在第一次请求时将json内容放入附在window上的属性中,然后在商店充水后将其清空。

Something along the lines of window.__BOOTSTRAP__ . 沿着window.__BOOTSTRAP__的线条window.__BOOTSTRAP__

store = createStore(appReducers, { content: yourJsonFileContents });

<Provider store={store}>
    <RootComponent />
</Provider>

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

相关问题 React Redux状态正在更新,但组件属性未更新 - React Redux State is Updating, but Component Props Not Updating React Redux将状态作为道具传递给组件 - React Redux pass state as props to component 通过react-redux connect调用`dispatch`后,如何立即用新道具设置react状态? - How to set react state with new props immediately after calling `dispatch` via react-redux connect? 如何将redux props分配给react js中的组件本地状态? - how to assign redux props to a component local state in react js? React Redux嵌套的智能组件-状态未映射到道具 - React redux nested smart component - state isn't mapped to props 反应:将formData道具合并到组件的状态(Redux) - React: Merging formData props to Component's state (Redux) 传递给道具的状态传播的变化停止在react-redux连接的组件上 - Change in state propagation to props stops at react-redux-connected component 如何以react-tippy将HTML props中的组件连接到redux形式 - How to connect component in html props in react-tippy to redux form 来自 React 组件 props 的 Redux 存储状态不反映 Redux 存储的状态值 - Redux store state from React component's props does not reflect redux store's state value 在 React class 组件中使用 typescript generic 将 props 连接到 state - Use typescript generic to connect props to state in React class component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM