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

        <bdo id='ApcCa'></bdo><ul id='ApcCa'></ul>

        <tfoot id='ApcCa'></tfoot>

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

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

        React.js - 默认 prop 不与 `null` 一起使用

        时间:2023-09-30
          <tbody id='12Sg5'></tbody>
      1. <legend id='12Sg5'><style id='12Sg5'><dir id='12Sg5'><q id='12Sg5'></q></dir></style></legend>

            <small id='12Sg5'></small><noframes id='12Sg5'>

              <bdo id='12Sg5'></bdo><ul id='12Sg5'></ul>

              <tfoot id='12Sg5'></tfoot>
                <i id='12Sg5'><tr id='12Sg5'><dt id='12Sg5'><q id='12Sg5'><span id='12Sg5'><b id='12Sg5'><form id='12Sg5'><ins id='12Sg5'></ins><ul id='12Sg5'></ul><sub id='12Sg5'></sub></form><legend id='12Sg5'></legend><bdo id='12Sg5'><pre id='12Sg5'><center id='12Sg5'></center></pre></bdo></b><th id='12Sg5'></th></span></q></dt></tr></i><div id='12Sg5'><tfoot id='12Sg5'></tfoot><dl id='12Sg5'><fieldset id='12Sg5'></fieldset></dl></div>
                • 本文介绍了React.js - 默认 prop 不与 `null` 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的 React 组件中有默认道具:

                  I have default props in my React component:

                  PropertyTitleLabel.defaultProps = {
                      bedrooms: 1,
                      propertyType: 'flat'
                  };
                  PropertyTitleLabel.propTypes = {
                      bedrooms: PropTypes.number,
                      propertyType: PropTypes.string
                  };
                  

                  但是当我将 null 传递给 bedrooms 时,例如:

                  But when I'm passing null to bedrooms like:

                  const bedrooms = null; // in real world API returns `null`
                  <Component bedrooms={bedrooms} />
                  

                  它没有被默认道具取代:(有什么想法吗?

                  It's not replaced with default prop :( Any ideas?

                  推荐答案

                  您可以将 null 值更改为 undefined 以使用默认值.

                  You can change the null value to undefined to use the default value.

                  <Component bedrooms={bedrooms || undefined} />
                  

                  这篇关于React.js - 默认 prop 不与 `null` 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Angular Component:没有模板替换选项? 下一篇:JSX 道具不应该使用 .bind() - 如何避免使用绑定?

                  相关文章

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

                  <tfoot id='Eey50'></tfoot>

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