【发布时间】:2020-09-13 13:28:51
【问题描述】:
这是我实现这一目标的尝试,但我似乎无法让它发挥作用:
import React from 'react'
/**
* Example of input:
* [
* {
* Context: React.createContext(),
* value: { test: "hello" }
* },
* {
* Context: React.createContext(),
* value: { test: "world" }
* }
* ]
*/
const recursive = (contexts, children) => {
if(contexts.length < 1) return children;
const { Context, value } = contexts[0];
return (
<Context.provider value={ value }>
{ recursive(contexts.shift()) }
</Context.provider>
)
}
function Contexts({ contexts, children }) {
return (
<>
{ recursive(contexts, children) }
</>
)
}
export default Contexts;
这甚至可以做到吗?如果可以,我能做到吗?
我使用递归函数的原因是因为有子组件需要存在于上下文提供程序中。
【问题讨论】:
-
一个问题肯定是你直接改变了道具。你应该在
shift之前slice
标签: reactjs react-hooks