我正在尝试使用 React Context 将函数传递给嵌套的子组件,这有效地允许子组件在按下时更新父组件的状态。
问题是我似乎遇到了错误'TypeError: render is not a function。 (在 render(newValue) 中,render 是 Array' 的一个实例,我的控制台中的错误是:'Warning: A context consumer was rendered with multiple children, or a child that isn't a function 。上下文使用者需要一个作为函数的子项。如果您确实传递了一个函数,请确保它周围没有尾随或前导空格。”
我查看了这个错误以及文档,但似乎没有答案可以回答我的问题,我不太明白为什么这不起作用。
编辑:我应该补充一点,渲染了多个子组件,因为它们是从对象数组构建的
片段:
家长:
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 }
value={ selected_attr.name } />
</IndexContext.Provider>
<OptionTile
tileColor='grey'
option="Quantity"
value="1" />
</Container>
在 OptionTile 中是我想在其中使用函数的子项:
const VariantTile = (props) => {
return (
<IndexContext.Consumer>
{({ handleChange }) => (
<TouchableOpacity onPress={handleChange(props.index)}>
<AsyncImage
source={ props.img_src }
placeholderColor="#fafafa"
style={{ flex: 1, width: null, height: 200 }}
/>
<Text>{ props.var_name }</Text>
<Text>{ props.price }</Text>
<Text>{ props.sku }</Text>
</TouchableOpacity>
)};
</IndexContext.Consumer>
)
};
上下文组件很简单:
const IndexContext = React.createContext();
export default IndexContext;
最佳答案
如错误所述,<Consumer>应该有唯一的 child ,它应该是一个函数。当它有多个子节点(包括文本节点)时会出现错误。
;在嵌入表达式导致问题之后。它不是表达式的一部分,使其成为表达式的一部分会导致语法错误。
应该是:
<IndexContext.Consumer>
{({ handleChange }) => (
<TouchableOpacity onPress={handleChange(props.index)}>
<AsyncImage
source={ props.img_src }
placeholderColor="#fafafa"
style={{ flex: 1, width: null, height: 200 }}
/>
<Text>{ props.var_name }</Text>
<Text>{ props.price }</Text>
<Text>{ props.sku }</Text>
</TouchableOpacity>
)}
</IndexContext.Consumer>
关于javascript - react 上下文 : TypeError: render is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54304594/
如果我有以下一段Ruby代码:classBlahdefself.bleh@blih="Hello"@@bloh="World"endend@blih和@@bloh到底是什么?@blih是Blah类中的一个实例变量,@@bloh是Blah类中的一个类变量,对吗?这是否意味着@@bloh是Blah的类Class中的一个变量? 最佳答案 人们似乎忽略了该方法是类方法。@blih将是常量Bleh的类Class实例的实例变量。因此:irb(main):001:0>classBlehirb(main):002:1>defself.blehirb
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我已经使用Stripe一年多了,基于RyanBates的RailsCast插曲发现here.但是,我的错误处理最近停止工作,而且我以前从未见过此错误。我最近开始在Ruby2.1上运行我的应用程序,据我所知,这就是问题所在。这是我的订阅模型中的一个实例方法:beginsave_with_stripe_paymentrescueStripe::InvalidRequestError=>elogger.error"Stripeerrorwhilecreatingcustomer:#{e.message}"logger.errore.backtrace.join("\n")errors.add
我有一个将某些事件写入队列的Rails3应用。现在我想在服务器上创建一个服务,每x秒轮询一次队列,并按计划执行其他任务。除了创建ruby脚本并通过cron作业运行它之外,还有其他稳定的替代方案吗? 最佳答案 尽管启动基于Rails的持久任务是一种选择,但您可能希望查看更有序的系统,例如delayed_job或Starling管理您的工作量。我建议不要在cron中运行某些东西,因为启动整个Rails堆栈的开销可能很大。每隔几秒运行一次它是不切实际的,因为Rails上的启动时间通常为5-15秒,具体取决于您的硬件。不过,每天这样做几
我在Ruby程序中有两个URI。一个肯定是绝对URI,另一个可能是绝对URI或相对URI。我想在第一个的上下文中将第二个转换为绝对URI,所以如果第一个是http://pupeno.com/blog第二个是/about,结果应该是http://pupeno.com/about.有什么想法吗? 最佳答案 Ruby的内置URI和Addressablegem,做这个简短的工作。我更喜欢Addressable,因为它功能更全面,但URI是内置的。require'uri'URI.join('http://pupeno.com/blog','/
我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan
玩转ruby,我已经:#!/usr/bin/ruby-w#WorldweatheronlineAPIurlformat:http://api.worldweatheronline.com/free/v1/weather.ashx?q={location}&format=json&num_of_days=1&date=today&key={api_key}require'net/http'require'json'@api_key='xxx'@location='city'@url="http://api.worldweatheronline.com/free/v1/weather.
是否可以不为lambda分配上下文?例如:classRuledefget_rulereturnlambda{putsname}endendclassPersonattr_accessor:namedefinit_rule@name="ruby"Rule.new.get_rule.call()#shouldsay"ruby"butsaywhatobjectofclassRull,doesnothavevariablename#orself.instance_eval&Rule.new.get_ruleendend我的目标是->没有上下文的存储过程对象,并在特定位置调用之前分配上下文。可能
我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文
我有一个用Rails3编写的站点。我的帖子模型有一个名为“内容”的文本列。在帖子面板中,html表单使用tinymce将“content”列设置为textarea字段。在首页,因为使用了tinymce,post.html.erb的代码需要用这样的原始方法来实现。.好的,现在如果我关闭浏览器javascript,这个文本区域可以在没有tinymce的情况下输入,也许用户会输入任何xss,比如alert('xss');.我的前台会显示那个警告框。我尝试sanitize(@post.content)在posts_controller中,但sanitize方法将相互过滤tinymce样式。例如