我正在尝试使用React Context将函数传递给嵌套的子组件,这有效地允许孩子在按下时更新父状态。
问题是我似乎收到错误信息'TypeError:render不是一个函数。(在render(newValue)中,render是Array的一个实例”,控制台中的错误显示为:“警告:使用多个子对象或不是函数的子对象来渲染上下文使用者。上下文使用者期望一个子函数。如果确实传递了函数,请确保函数周围没有尾随或前导空格。
我已经查看了这个错误以及文档,但是似乎没有答案可以回答我的问题,我无法完全弄清为什么它不起作用。
编辑:我应该补充一点,有多个子组件呈现,因为它们是从对象数组构建的
片段:
上级:
class Product extends Component {
state = {
selected_ind: 0
};
handleContextChange = selected_ind => {
this.setState({selected_ind});
};
render() {
const contextValue = {
data: this.state,
handleChange: this.handleContextChange
};
//Desconstruct props for ease of use
const {
attr_data,
variant,
product_name } = this.props;
return (
<Container>
<Heading>Product Options</Heading>
<IndexContext.Provider value={contextValue}>
<OptionTile
tileColor='grey'
onPress={ () => this.props.navigation.navigate('Variants', {
attr_data: attr_data,
selected_ind: this.state.selected_ind
})} //Replace with named function
option="Variant"
selected_ind={ this.state.selected_ind …Run Code Online (Sandbox Code Playgroud) I'm trying to implement authenticated routes (as per React router docs) alongisde dynamic theming from my top level component (essentially the theme changes when a menu item is selected but no redirection occurs) - this means a small amount of state management is needed在我的顶级组件中。
function App() {
const [ state, setState ] = React.useState({
current_theme: themes['blue']
});
const [ logged_in, setLoggedIn ] = React.useState( !!Cookie.get('JWT') );
function selectTheme( theme ) {
setState({
current_theme: themes[theme]
});
};
return (
<MuiThemeProvider theme={state.current_theme}> …Run Code Online (Sandbox Code Playgroud)