首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果新值在相同的行和标题下,React中的动态表不会重新呈现

如果新值在相同的行和标题下,React中的动态表不会重新呈现
EN

Stack Overflow用户
提问于 2021-07-21 08:49:16
回答 1查看 37关注 0票数 1

这是从我的函数组件返回的表

代码语言:javascript
复制
<table class="table"> {/* Consonant Table */}
                    <tr>
                        <th colSpan="2">---</th>
                        {headersPOA.map( e => //headers for POA
                            <th colSpan="2">{e}</th>)}
                    </tr>

                    {headersMOA.map(c => //headers for MOA
                    <tr>
                        <th colSpan="2">{c}</th>
                        {addRowSpaces(getRowContent(c)).map(d => //row content with spaces
                            <td align= "center" width= "60px" height= "40px">
                                <button>{d.symbol}</button>
                            </td> 
                            )}
                    </tr>)}
                </table>

以下是我用来填充它的函数:

代码语言:javascript
复制
    const [headersPOA, setHeadersPOA] = useState([]);
    const [headersMOA, setHeadersMOA] = useState([]);

    function getRowContent(moaToTest){// get consonant table row contents
        let row = Object.values(phonemes)
            .filter(phoneme => phoneme.moa === moaToTest)
        return row;
    }

    function addRowSpaces(rowPhonemes) {// add spacing to consonant table rows
        let result = []

        headersPOA.forEach(element => { //check what phoneme to put in each column
            var foundUnvoiced = false; //no unvoiced phonemes fit in this column yet
            var foundVoiced = false; //no voiced phonemes fit in this column yet
            var phonemesInColumn = [] //both voiced and unvoiced phonemes in that column
            for(var i = 0; i < rowPhonemes.length; i++) { //check if any of the phoneme in that row matches the column
                if(rowPhonemes[i].poa === element) { //the phoneme has that poa
                    phonemesInColumn.push(rowPhonemes[i])
                    rowPhonemes.splice(i, 1)
                }
            }
            for(var i = 0; i < phonemesInColumn.length; i++){//check if unvoiced of unvoiced
                if(phonemesInColumn[i].sol === "Unvoiced"){
                    result.push(phonemesInColumn[i])
                    phonemesInColumn.splice(i, 1)
                    foundUnvoiced = true
                }
            }
            if(foundUnvoiced === false){
                result.push("")
            }

            for(var i = 0; i < phonemesInColumn.length; i++){//check if voiced of unvoiced
                if(phonemesInColumn[i].sol === "Voiced"){
                    result.push(phonemesInColumn[i])
                    phonemesInColumn.splice(i, 1)
                    foundVoiced = true
                }
            }
            if(foundVoiced === false){
                result.push("")
            }
        });
        return result;
    }

    function getConsonantHeadersPOA() {//get unique POA
        let uniquePOA = [...new Set(Object.values(phonemes)
            .filter(phoneme => phoneme.type === "C")
            .map(phoneme => (phoneme.poa)))];

        return uniquePOA;
    }

    function getConsonantHeadersMOA() {//get unique MOA
        let uniqueMOA = [...new Set(Object.values(phonemes)
            .filter(phoneme => phoneme.type === "C")
            .map(phoneme => (phoneme.moa)))];

        return uniqueMOA;
    }

这是我的桌子的照片。当我提交一个新值时,它被正确地添加了,但是如果新的音素与另一个音素在相同的标题下并且在相同的行中,则表不会更新。如果我添加一个浊音(在右边),腭部,塞音,它会被添加到表中,但不会被添加到表中,因为它的C在相同的位置,但不是浊音(在左边)。有人知道为什么会这样吗?我的表格:https://i.stack.imgur.com/0hi8I.png

EN

回答 1

Stack Overflow用户

发布于 2021-07-22 04:50:45

我找到问题了。我有这一行rowPhonemes.splice(i,1),这是我在以前的版本中需要的,但在我做了更改之后,它删除了它读取的第二个项目,它共享相同的位置和方式。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68462812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档