jsx

React is rendering [object object] rather than the JSX

久未见 提交于 2019-12-05 11:15:06
问题 I'm trying to render out journal entries on my site with an object (not array) and I am running into an issue, here is my current code populateJournal(){ const j = Object.values(this.state.journal); var journalEntries = ''; for (var i = 0; i < j.length; i++){ journalEntries+= <div> <h3>{j[i].title} - {j[i].date}</h3> <p>{j[i].entry}</p> </div>; } return(<div>{journalEntries}</div>); } When i call this function it renders "<div>[object object]</div>" and the text between the divs is plain text

How to do a nested if else statement in reactjs JSX?

浪子不回头ぞ 提交于 2019-12-05 08:31:37
问题 I wanted to know if its possible to do nested if else if in reactjs jsx ? I have tried various different ways and I am unable to get it to work. I am looking for if (x) { loading screen } else { if (y) { possible title if we need it } main } I have tried this but I can not get it to render. I have tried various ways. It always breaks once I add the nested if. { this.state.loadingPage ? (<div>loading page</div>) : (<div> this.otherCondition && <div>title</div> <div>body</div> </div>) } Update

How to iterate nested objects and render inside jsx?

柔情痞子 提交于 2019-12-05 07:22:13
How can I render a nested map inside my jsx component? I need to do the equivalent of a javascript for(key in groupItem){} See below. class MyComponent extends React.Component { render () { var options = this.props.options; return ( <div> {options.map(function(groupItem, key){ return ( /* Unexpected Token if using groupItem.map? {groupItem.map(function(){return })} */ )})} </div> ) } } Dropdown.defaultProps = { options:[{ 'groupX':{ 'apple':'lovely green apple', 'orange':'juicy orange', 'banana':'fat banana' } }] } JSON.stringify(groupItems) === { 'groupX':{ 'apple':'lovely green apple',

vue中引入typescript开发使用vue.extend()和vue.class-component

落爺英雄遲暮 提交于 2019-12-05 06:39:34
最近尝试了一下 TypeScript,试着把一个 Vue 项目改成了 TypeScript 的,感觉还不错 目前 Vue 和 TypeScript 的配合还不算很完美,Vuex 和 TypeScript 的配合挺糟糕的,尝试需要谨慎 如果想体验一下的话,强烈建议你用 vue-cli 3 直接生成项目目录,这样会少挺多配置,比如配 tsconfig 什么的,在用 vue-cli 的时候选择 TypeScript 就好 如果想自己体验从 0 开始配置请参考这个文档 TypeScript-Vue-Starter 初始配置 这里不提初始配置,因为 vue-cli 已经默认配置好了,在根目录下,有个 tsconfig.json 的文件,在 src 的文件夹下面,有 shims-tsx.d.ts, shims-vue.d.ts 文件 shims-vue.d.ts 这个文件,主要用于 TypeScript 识别.vue 文件,Ts 默认并不支持导入 vue 文件,这个文件告诉 ts 导入.vue 文件都按 VueConstructor<Vue> 处理,因此导入 vue 文件必须写.vue 后缀,但是这样同样的也会造成,就算你写的导入的 .vue 文件的路径就算是错的,静态检测也不会检测到错误,如果你把鼠标放上面你会看到错误的路径就是指向这个文件,因为你定义了这个模块是所有 .vue

vue中使用jsx

若如初见. 提交于 2019-12-05 03:17:26
vue中使用jsx 为什么需要使用jsx呢?这个需要搞清楚 其实vue官方也说了,对于那些非常多v-if v-else的情况,就可以尝试使用render函数或者jsx,不过render函数写简单的结构还行,结构复杂了就很蛋疼了,而既然用到render了,肯定是有一些复杂的逻辑判断,结构肯定简单不了,所以用jsx就是一个比较好的选择了 今天自己尝试了一下,也是借鉴了网上的一些例子,不过在使用图片的时候发现事情好像有点难搞 <script> import img_more from '../assets/images/pk/icon-more.png'; export default { name: 'More', props: { type: { required: true } }, data() { return { text: 'xxxxxxxjsx', role: 1 } }, render() { return ( <div class={{ btn: true, 'btn-success': this.type === 'success', 'btn-danger': this.type === 'danger', 'btn-warning': this.type === 'warning' }} > {this.text} /*这种写法是可以的,图片路径通过变量传递进来

Specify/Set width and height on a react-boostrap modal

不问归期 提交于 2019-12-05 02:57:31
How can I apply a dynamic width and height to a react-bootstrap modal window? I have checked the react-bootstrap docs here but could not figure out how to do that. Actually the value of width and height props would be dynamic (could be any values) as this will be a reusable component in my app (to be used on many pages) thus can't apply width/height through some CSS class. 'bsSize' property as mentioned in docs also not working, although predefined sizes of xs, md, lg is not what I exactly want, rather I need width and height to be set on modal via props. Here is my sample JSX code: var

How to loop through object in JSX using React.js

不问归期 提交于 2019-12-05 02:04:52
So I have a React.js component, and I want to loop through an object I import to add HTML options to it. Here is what I tried, which is both ugly and does not work: import React from 'react'; import AccountTypes from '../data/AccountType'; const AccountTypeSelect = (props) => { return ( <select id={props.id} className = {props.classString} style={props.styleObject}> <option value="nothingSelected" defaultValue>--Select--</option> { $.each(AccountTypes, function(index) { <option val={this.id}>this.name</option> }) } </select> ); }; export default AccountTypeSelect; I received this error in the

forEach over es6 Map in JSX

你。 提交于 2019-12-05 01:54:15
I had a javascript array that was rendering components using array.map . I switched this array to an es6 Map in order to be able to use key-value pairs to find items more easily, and switched from a .map to a forEach over the Map. Inside the forEach I call a render method that returns a React component, but it isn't being rendered. How do I render a component inside the forEach ? <div className='gallery__items'> {resultsByGuid.forEach((result, index) => { key++; this.renderGalleryItem(result, key); })} </div> Here is the renderGalleryItem method: renderGalleryItem = (item, index) => { const {

How to have nested loops with map in JSX?

痞子三分冷 提交于 2019-12-04 23:14:08
I can't achieve to have two nested map : render() { return ( <table className="table"> <tbody> {Object.keys(this.state.templates).map(function(template_name) { return ( <tr key={template_name}><td><b>Template: {template_name}</b></td></tr> {this.state.templates[template_name].items.map(function(item) { return ( <tr key={item.id}><td>{item.id}</td></tr> ) })} ) })} </tbody> </table> ) } This gives a SyntaxError: unknown: Unexpected token . How do you nest map calls in JSX? You need to wrap it inside an element. Something like this (I've added an extra tr due to the rules of tables elements):

React - Slide fixed navbar up on scroll down and slide down on scroll up

半城伤御伤魂 提交于 2019-12-04 15:51:53
tl;dr Scroll down for the solution that worked for me! How to implement a slide up and down on a fixed navbar in react? What is the better approach using refs or using the componentDidMount lifecycle hook? hideNav = (navbar) => { const hide = () => { let lastScrollTop = 0; const currentScrollTop = navbar.scrollTop; // scroll down if (currentScrollTop > lastScrollTop) { navbar.classList.add('hidden'); } else { // scroll up navbar.classList.remove('hidden'); } lastScrollTop = currentScrollTop; }; window.addEventListener('scroll', hide); }; ... further down in the render method: render() { return