javascript - 将 jsx 内容传递给 redux 状态并从那里渲染它

标签 javascript reactjs redux

我无法弄清楚如何将 JSX 传递到我的 redux 状态,即对于全局使用的模态组件并具有其 redux 状态,其中一个参数是 content 这样的内容可以更新为包含 JSX代码。

目前我正在让它呈现正确的内容,但似乎函数调用不正确,我也收到以下错误:

invariant.js:38 Uncaught Error: Objects are not valid as a React child (found: object with keys {dispatchConfig, _targetInst, isDefaultPrevented, isPropagationStopped, _dispatchListeners, _dispatchInstances, nativeEvent, type, target, currentTarget, eventPhase, bubbles, cancelable, timeStamp, defaultPrevented, isTrusted, view, detail, screenX, screenY, clientX, clientY, ctrlKey, shiftKey, altKey, metaKey, getModifierState, button, buttons, relatedTarget, pageX, pageY}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of styled.div.

有很多错误:

warning.js:36 Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the property nativeEvent on a released/nullified synthetic event. This is set to null. If you must keep the original synthetic event around, use event.persist(). See https://fbme(replaced this as so doesn't allow links to fb)/react-event-pooling for more information.

示例实现:

从页面调用以显示模态并向其添加内容的函数

  onToggleModal = () => {
    this.props.modalToggle(
      (<TopUp account={getSession().accounts[0] || {}} />)
    );
  }

this.props.modalToggle 是这样的 redux Action :

export const modalToggle = (content = '') => ({
  type: MODAL_TOGGLE,
  payload: content
});

然后我尝试在我的模态容器中呈现此类内容:

return (
 <div>{this.props.content}</div>
)

我将 React 导入到我的 reducer 中,希望能解决 jsx 问题,但没有成功。组件似乎也作为某种奇怪的对象传递给 reducer。

最佳答案

Redux 状态只能包含普通对象和数据类型,因此 JSX 对象可能会导致问题。此外,将 JSX 对象(基本上是一个 View )作为状态的一部分很可能不是一个好主意。

相反,您应该传递呈现最终 View 所需的任何数据。我认为这会起作用:

onToggleModal = () => {
  this.props.modalToggle(getSession().accounts[0] || {});
}

export const modalToggle = (account = {}) => ({
  type: MODAL_TOGGLE,
  account: account
});

return (
 <div><TopUp account={account} /></div>
)

关于javascript - 将 jsx 内容传递给 redux 状态并从那里渲染它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41167556/

相关文章:

redux - 你可以使用 combineReducers 来组合深度状态 reducer 吗?

javascript - 如何使用 d3 v4 读取 CSV?

node.js - 无法使用远程服务器在本地进行身份验证

javascript - 如何通过 javascript 从 asp 服务器脚本获取 session 变量

javascript - React 组件未渲染 Axios 请求的响应

reactjs - 如何使用react-markdown将Markdown转换为HTML,并将其导出为HTML代码?

javascript - React Redux 骨架从状态返回未定义的值

javascript - 派发一个 Redux Action 并将后续 Action 作为有效负载以显示 Material UI 的 snackbar 或对话框

php - 更改 PHP 脚本中的变量并再次运行函数,无需重新加载页面

javascript - 为什么从 MySQL 和 jQuery .click 函数检索的数据表行不可单击?