首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SparkLines ReactJS -接收NaN

SparkLines ReactJS -接收NaN
EN

Stack Overflow用户
提问于 2020-01-05 13:09:15
回答 2查看 705关注 0票数 0

试图找出ReactJS。也就是说,到了应该做SparkLines的时刻。但由于某种原因没有显示值。有什么不对的?

杰森:

代码语言:javascript
复制
[
    {
        "playersCount": "41170",
        "playersCountToday": "41",
        "playersCountAll": "45232",
        "countDailyChecksAll": [
            "41",
            "125",
            "68",
            "26",
            "41",
            "41",
            "66"
        ]
    }
]

代码:

代码语言:javascript
复制
class CountMain extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            post: []
        };
    }

    componentDidMount() {
        this.fetchPost();
        this.timer = setInterval(()=> this.fetchPost(), 5000)
    }

    componentWillUnmount() {
        clearInterval(this.timer);
    }    

    async fetchPost() {
        fetch('https://example.com/json')
        .then(res => {
            return res.json();
        })
        .then(data => {
            this.setState({
                post: data
            });
        })
        .catch(err => {
            console.log(err);
        });
    }

    render() {
        const playersCount = this.state.post.length === 0 ? <span><i className="fas fa-spinner fa-spin"></i></span> : <span>{this.state.post[0].playersCount}</span>;
        const playersCountToday = this.state.post.length === 0 ? <span><i className="fas fa-spinner fa-spin"></i></span> : <span>{this.state.post[0].playersCountToday}</span>;
        const playersCountAll = this.state.post.length === 0 ? <span><i className="fas fa-spinner fa-spin"></i></span> : <span>{this.state.post[0].playersCountAll}</span>;

        const countDailyChecksAll = this.state.post.length === 0 ? 0 : this.state.post[0].countDailyChecksAll.join(',');
        //console.log(countDailyChecksAll);

        return (
            <div className="row">
                <div className="col-xl-3 col-md-6 col-sm-12">
                    <div className="box-playerCount rcc--stats">
                        <div className="box-cont">
                            <img className="unselectable" src="myimage.jpg" />
                            <h3 className="box-title">Test Column</h3>
                            <ul className="list-inline two-part">
                                <li>
                                    <Sparklines data={[{countDailyChecksAll}]} margin={6}> //this line error
                                        <SparklinesLine style={{ strokeWidth: 6, stroke: "#4c90e5", fill: "none" }} />
                                    </Sparklines>                                    
                                </li>
                                <li className="numbers text-right animated fadeInLeft">
                                    {playersCountAll}
                                </li>
                            </ul>                                
                        </div>
                    </div>
                </div>
            </div>
        )
    }
}

错误:

警告:接收到用于NaN属性的cy。如果这是预期的,则将值强制转换为字符串。

错误:属性cy:预期长度,"NaN“。

.NaN:2323错误:属性点:期望值,"6 NaN“。

console.log输出:

41 125 68 26 41 41 66

我做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2020-01-05 15:30:35

在react中,应该在Sate中定义动态变量。

State是一个JavaScript对象,它存储组件的动态数据并确定组件的行为。因为状态是动态的,它使组件能够跟踪呈现之间的更改信息,并使其具有动态和交互性。

例如,而不是

代码语言:javascript
复制
const countDailyChecksAll = this.state.post.length === 0 ? 0 : 
this.state.post[0].countDailyChecksAll.join(',');

使用,例如:

代码语言:javascript
复制
 this.setState({countDailyChecksAll: this.state.data[i].countDailyChecksAll})

临时代码沙箱示例

票数 1
EN

Stack Overflow用户

发布于 2020-01-05 15:42:40

data应该是一个数字数组。您有一个字符串数组,您要将其转换为一个以逗号分隔的String,然后将其包装到一个数组中。那是不正确的。

代码语言:javascript
复制
// convert to numbers
const countDailyChecksAll = this.state.post[0].countDailyChecksAll.map(parseFloat);

<Sparklines data={countDailyChecksAll} ... >
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59600199

复制
相关文章

相似问题

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