Javascript 无法在本机反应中分配给#<Object> 的只读属性“道具”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33900933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot assign to read only property 'props' of #<Object> in react native
提问by bozzmob
Cannot assign to read only property 'props' of #
无法分配给 # 的只读属性“道具”
I checked #1654with no success. Please have a look at the issue in more detail here-
我检查了#1654没有成功。请在此处更详细地查看该问题-
Basically what I am doing is using a Navigator to move from index page to ApplistGridView page. I see the navigation is successful(from logs) but, even before I see the screen I face this issue.
基本上我正在做的是使用导航器从索引页面移动到 ApplistGridView 页面。我看到导航成功(从日志中)但是,甚至在我看到屏幕之前我就遇到了这个问题。
And chrome debug messages-
和 chrome 调试消息-
Code is posted at GithubHaven't found a solution. What am I doing wrong?
代码发布在Github还没有找到解决方案。我究竟做错了什么?
采纳答案by Thomas
You cannot push to props this.props.nav.push({id: 'Applist', index: 2});
since component properties are read-only, as the error states. Props can only be derived from a parent component, but cannot be modified.
您不能推送到 props,this.props.nav.push({id: 'Applist', index: 2});
因为组件属性是只读的,如错误所述。props 只能从父组件派生,但不能修改。
EDIT: This article is a great starting point for people confused in this matter as I was a while ago:) https://reactjs.org/docs/thinking-in-react.html
编辑:这篇文章是一个很好的起点,因为我不久前在这件事上感到困惑:) https://reactjs.org/docs/thinking-in-react.html
回答by CPHPython
If this is happening to you not because of propsbut because of state, you may be trying to do an assignment like this:
如果这不是因为props而是因为state发生在你身上,你可能会尝试做这样的分配:
this.state.nav = 'whatever';
Remember that assignments to state
in React should be like:
请记住,state
React中的赋值应该是这样的:
this.setState({
nav: 'whatever'
});
回答by Ihsan
You write this.props.nav.push({id: 'Applist', index: 2});
你写 this.props.nav.push({id: 'Applist', index: 2});
But this.props
is read-only; you cannot modify it.
但是this.props
是只读的;你不能修改它。
If you want to pass some value to the parent react component, then you can use this.prop.onSomeEvent(value)
in a child component. Then handle the push process in the parent component.
如果您想将一些值传递给父 react 组件,则可以this.prop.onSomeEvent(value)
在子组件中使用。然后在父组件中处理推送过程。