我尝试从 rails 加载 json,并将其传递给 redux createStore。
let initialStore = window.INITIAL;
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const store = createStoreWithMiddleware(rootReducer, initialStore);
但我总是未定义,或者只是 window.INITIAL 存储后的返回值。
首先,使用空对象存储加载,然后调度 fetch 操作,并且我使用 json 更新存储,但是当我尝试在空对象上调用 { product.title } 之类的东西时,我已经出错了。无论我做什么,我都无法在 redux 开始之前加载 json,即使是全局数据,就像这样。
(function() {
$.getJSON('http://localhost:3000/products.json', function(data) {
return window.INITIAL = data;
});
});
Api 和控制器很简单。
def index
products = Product.all
render json: products
end
你怎么处理这个?我想在没有像 react-rails 等任何宝石的情况下做到这一点,我无法将所有初始状态都传递给一个地方的 Root 组件。