<i id='UmCWl'><tr id='UmCWl'><dt id='UmCWl'><q id='UmCWl'><span id='UmCWl'><b id='UmCWl'><form id='UmCWl'><ins id='UmCWl'></ins><ul id='UmCWl'></ul><sub id='UmCWl'></sub></form><legend id='UmCWl'></legend><bdo id='UmCWl'><pre id='UmCWl'><center id='UmCWl'></center></pre></bdo></b><th id='UmCWl'></th></span></q></dt></tr></i><div id='UmCWl'><tfoot id='UmCWl'></tfoot><dl id='UmCWl'><fieldset id='UmCWl'></fieldset></dl></div>

      • <bdo id='UmCWl'></bdo><ul id='UmCWl'></ul>
      <legend id='UmCWl'><style id='UmCWl'><dir id='UmCWl'><q id='UmCWl'></q></dir></style></legend>
    1. <small id='UmCWl'></small><noframes id='UmCWl'>

      1. <tfoot id='UmCWl'></tfoot>

        React Native - 将父母的状态传递给孩子的麻烦

        React Native - Trouble passing parent#39;s state to child(React Native - 将父母的状态传递给孩子的麻烦)
        <tfoot id='EI5eL'></tfoot>
          <tbody id='EI5eL'></tbody>
            <bdo id='EI5eL'></bdo><ul id='EI5eL'></ul>

            <small id='EI5eL'></small><noframes id='EI5eL'>

            <i id='EI5eL'><tr id='EI5eL'><dt id='EI5eL'><q id='EI5eL'><span id='EI5eL'><b id='EI5eL'><form id='EI5eL'><ins id='EI5eL'></ins><ul id='EI5eL'></ul><sub id='EI5eL'></sub></form><legend id='EI5eL'></legend><bdo id='EI5eL'><pre id='EI5eL'><center id='EI5eL'></center></pre></bdo></b><th id='EI5eL'></th></span></q></dt></tr></i><div id='EI5eL'><tfoot id='EI5eL'></tfoot><dl id='EI5eL'><fieldset id='EI5eL'></fieldset></dl></div>

                  <legend id='EI5eL'><style id='EI5eL'><dir id='EI5eL'><q id='EI5eL'></q></dir></style></legend>

                1. 本文介绍了React Native - 将父母的状态传递给孩子的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  React 新手在从父组件(本例中为 App)传递状态和函数以及从子组件(本例中为 Main)访问时遇到一些问题.我确定这是一两个非常简单的错误,我在哪里被绊倒了?

                  a React newbie having some issues passing down states and functions from Parent Component (App in this case) and accessing from Child Components (Main in this case). I'm sure it's one or two really simple mistakes, where am I getting tripped up?

                  这是项目结构:

                  App
                   |__ Rootstack
                         |
                         |__Favorites
                         |__Main
                  

                  这是精简后的代码:

                  class Main extends React.Component {
                    render() {
                        return (
                        <View>
                           <Text>{this.props.favoritesList.length}</Text>
                           <Card>
                                onSwiped={() => {this.props.updateArray}} //the idea being that on a swipe, updateArray would add 1 to the 'favoriteList' in the parents state, and the favoritesList.length would update by +1.
                           </Card>
                        </View>
                          );
                      }
                  }
                  
                  class Favorites extends React.Component {
                            ....
                      }
                  
                  const RootStack = StackNavigator(
                    {
                      Main: {
                        screen: Main},
                      Favorites: {
                        screen: Favorites}
                    },
                    {
                      initialRouteName: 'Main'
                    }
                  );
                  
                  
                  export default class App extends Component<{}> {
                    constructor(props) {
                        super(props);
                        this.state = {
                          favoritesList: []
                        };
                      this.updateArr = this.updateArr.bind(this); //don't know if this is necessary?
                      }
                  
                    updateArr=()=>{this.setState({ favoritesList: 
                        [...this.state.favoritesList, 'new value']})};
                  
                    render() {
                        return <RootStack {...this.state} updateArray={this.updateArr}/>;
                      }
                    }
                  

                  我得到的错误是 -- 有什么想法吗?提前致谢!

                  Error I'm getting is -- any ideas? Thanks in advance!

                  推荐答案

                  1-

                  这是不对的

                         <Card  ... props should be here!!---  >
                            onSwiped={() => {this.props.updateArray}} //the idea being that on a swipe, updateArray would add 1 to the 'favoriteList' in the parents state, and the favoritesList.length would update by +1.
                       </Card>
                  

                  正确答案是:

                       <Card  
                         onSwiped={() => {this.props.updateArray}} //the idea being that on a swipe, updateArray would add 1 to the 'favoriteList' in the parents state, and the favoritesList.length would update by +1.
                        >
                      </Card>
                  

                  错误的意思是Card的孩子应该是一个对象而不是.....

                  The meaning of the error is that the child of Card is expected to be an object and not .....

                  2-

                  this.updateArr = this.updateArr.bind(this); 不是必需的,因为 updateArr = () =>;... 是用 es6 编写的

                  this.updateArr = this.updateArr.bind(this); is not necessary since updateArr = () => ... is written in es6

                  这篇关于React Native - 将父母的状态传递给孩子的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)

                  <small id='P896h'></small><noframes id='P896h'>

                  1. <tfoot id='P896h'></tfoot>

                    • <bdo id='P896h'></bdo><ul id='P896h'></ul>

                      <i id='P896h'><tr id='P896h'><dt id='P896h'><q id='P896h'><span id='P896h'><b id='P896h'><form id='P896h'><ins id='P896h'></ins><ul id='P896h'></ul><sub id='P896h'></sub></form><legend id='P896h'></legend><bdo id='P896h'><pre id='P896h'><center id='P896h'></center></pre></bdo></b><th id='P896h'></th></span></q></dt></tr></i><div id='P896h'><tfoot id='P896h'></tfoot><dl id='P896h'><fieldset id='P896h'></fieldset></dl></div>
                        <legend id='P896h'><style id='P896h'><dir id='P896h'><q id='P896h'></q></dir></style></legend>
                          <tbody id='P896h'></tbody>