Reactjs 将存储手动传递给组件时组件未重新提交(react redux)
我正在构建一个从json定义动态呈现react组件的应用程序。某些组件需要使用数据上下文呈现。然后,这些组件需要获取外部数据。我以前有一个简单的解决方案,用DataComponent包装这些组件,DataComponent在装载期间发出请求,然后设置状态。这种方法有效Reactjs 将存储手动传递给组件时组件未重新提交(react redux),reactjs,react-redux,Reactjs,React Redux,我正在构建一个从json定义动态呈现react组件的应用程序。某些组件需要使用数据上下文呈现。然后,这些组件需要获取外部数据。我以前有一个简单的解决方案,用DataComponent包装这些组件,DataComponent在装载期间发出请求,然后设置状态。这种方法有效 componentWillMount = () => provider .get() .then(data => this.setState({data})) 现在,我尝试使用redux提出一个解
componentWillMount = () =>
provider
.get()
.then(data => this.setState({data}))
现在,我尝试使用redux提出一个解决方案,这样所有的数据模式都有一个reducer,每个组件都不需要管理自己的数据(这些数据可能与其他组件来自同一个源)。存储被正确初始化,我可以看到每当调度一个操作时,存储状态都被正确更新(我使用redux devtools进行验证,没有状态发生变化)。但是,该组件不会使用新数据进行更新。初始调用后,render
函数不会重新调用
这里有一个类似于我的代码:
import React from 'react'
import {connect} from 'react-redux'
import PropTypes from 'prop-types'
// Component: React.Component
// store: redux-store
// schema: String
export default function connectedComponent(Component, store, schema) {
class DataComponent extends React.Component {
static propTypes = {
// eslint-disable-next-line react/forbid-prop-types
data: PropTypes.any
}
static defaultProps = {
data: null
}
render = () => {
if (this.props.data) {
return this.renderComponent(Component, this.props.data)
}
return (<span>empty</span>)
}
}
renderComponent = (Component, data) => ...
// eslint-disable-next-line react/no-multi-comp,react/display-name
return (props) => {
const ConnectedData = connect(mapStateToProps(schema))(DataComponent)
return (
<ConnectedData {...props}
store={store}/>)
}
}
const mapStateToProps = (schema) => (state) => ({
data: state[schema][schema]
})
在应用程序内部,还有另一个redux存储来管理渲染器状态
编辑1:
添加github repo以演示该问题
编辑2:
react-redux版本为5.0.2,发现与针对自定义提供的存储的订阅相关的错误,该错误至少在5.0.6上已修复react-redux版本为5.0.2,发现与针对自定义提供的存储的订阅相关的错误,该错误至少在5.0.6上已修复
将react redux更新为5.0.6修复了该问题。此处描述了该错误react redux版本为5.0.2,发现存在与针对自定义提供的存储的订阅相关的错误,该错误至少已在5.0.6上修复 将react redux更新为5.0.6修复了该问题。这里描述了这个bug
import {createStore} from 'redux'
import {combineReducers} from 'redux'
import reduce from 'lodash/reduce'
import has from 'lodash/has'
export default (schemas) => createStore(
createReducers(schemas),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
const createReducers = (schemas) => combineReducers(reduce(schemas, accumulateReducers, {}))
const accumulateReducers = (reducers, schema) => ({...reducers, [schema.id]: createReducer(schema)})
const createReducer = (schema) => {
return (state = getDefault(schema), {type, response}) => {
switch (type) {
case `${schema.id}_GET_SUCCESS`:
return {...state, [schema.id]: response}
default:
return state
}
}
}
const getDefault = (schema) => ({
[schema.id]: getDefaultValue(schema)
})
const getDefaultValue = (schema) => {
if (has(schema, 'defaultValue')) {
return schema.defaultValue
}
switch (schema.type) {
case 'array':
return []
case 'object':
return {}
default:
return null
}
}