React : Best way to put dummy content in a component - reactjs

I want to prepare my component to receive an JSON object from the backend.
So in my main component, I've got a state :
constructor(props) {
super(props);
this.state = {
contributions : {
[
{
name: 'Test',
value: '1',
},
{
name: 'Test2',
value: '12',
},
]
}
render() {
const { contributions } = this.state;
return (
<MyComponent contributions={contributions} />
);
}
So now, I want to know the best solution to render my object in MyComponent so I can have for output :
<div>
<span class="name">Test</span>
<span class="value">1</span>
</div>
<div>
<span class="name">Test2</span>
<span class="value">12</span>
</div>

JSON objects are key-value pairs. So if you're saving the JSON to your state, it can look something like
this.state = {
contributions : {
"nameValuePairs":[
{
"name": 'Test',
"value": '1',
},
{
"name": 'Test2',
"value": '12',
},
]
}
Now to map through the objects you can do something like
{this.state.contributions.nameValuePairs.map((item)=>(<TestChild item={item}/>))
Bascially your parent would look something like
import React, { Component } from 'react';
import TestChild from './TestChild'
class Test extends Component {
constructor(props) {
super(props);
this.state = {
contributions : {
"nameValuePairs":[
{
"name": 'Test',
"value": '1',
},
{
"name": 'Test2',
"value": '12',
},
]
}
}
}
render() {
return (
<div>
{this.state.contributions.nameValuePairs.map((item)=>(<TestChild item={item}/>))}
</div>
);
}
}
export default Test;
and your child component can have inside it, something like
render () {
return (
<div>{this.props.item.name}</div>
<div>{this.props.item.value}</div>
)
}

MyComponent
<div>
<span class="name">{this.props.name}</span>
<span class="value">{this.props.value}</span>
</div>
ParentComponent
constructor(props) {
super(props);
this.state = {
contributions : {
[
{
name: 'Test',
value: '1',
},
{
name: 'Test2',
value: '12',
},
]
}
render() {
const { contributions } = this.state;
return (
contributions && contributions.length && contributions.map(contrib => (
<MyComponent {...contrib) />
))
);
}

Related

State exists but not a function error happens. this.setState is not a function

State is existing but "not a function error" happens.
I am developing a component with React Tag.
The function is exists
I have no idea what it is wrong..
Error says
TypeError: this.setState is not a function
class ReagtTagSample extends Component {
constructor(props) {
super(props);
this.state = {
tags: [{ id: 'Yugoslavia', text: 'Yugoslavia' }, { id: 'India', text: 'India' }],
suggestions: [
{ id: "England", text: "England" },
{ id: "Mexico", text: "Mexico" },
],
};
handleAddition(tag) {
this.setState((state) => ({ tags: [...state.tags, tag] }));
}
handleDrag(tag, currPos, newPos) {
const tags = [...this.state.tags];
const newTags = tags.slice();
newTags.splice(currPos, 1);
newTags.splice(newPos, 0, tag);
this.setState({ tags: newTags });
}
render() {
const { test } = this.props;
const { tags, suggestions } = this.state;
<ReactTags
tags={tags}
suggestions={suggestions}
handleDelete={this.handleDelete}
handleAddition={this.handleAddition}
handleDrag={this.handleDrag}
delimiters={delimiters}
/>
handleAddition should be an arrow function to rebind this otherwise is not a class reference
handleAddition = (tag) => {
this.setState((state) => ({ tags: [...state.tags, tag] }));
}
same applies for handleDrag

Ant Design for React : Show/Hide particular column

I need a bit of help here, In an Ant Design table, I need to hide/show a particular column of a table depending on a state value. In the given sandbox link, I need to hide the surname column whenever the switch is Off and show when the switch is On.
Please, someone, look into this, and help me out.
Reference: https://codesandbox.io/s/purple-sun-1rtz1?file=/index.js
There is a working code, but it should be more customize, interactivize, and refactorize depending on your need:
// You can also modify the data in the `handleChnage`
// Or conditionally display it like here:
class EditableTable extends React.Component {
state = {
surNameShow: false
};
constructor(props) {
super(props);
this.columns = [
{
title: "Name",
dataIndex: "name",
width: "30%"
},
{
title: "Surname",
dataIndex: "surname",
width: "30%"
}
];
this.state = {
dataSource: [
{
key: "0",
name: "Edward 1",
surname: "King 1"
},
{
key: "1",
name: "Edward 2",
surname: "King 2"
}
]
};
}
handleChnage = key => {
this.setState({ surNameShow: !this.state.surNameShow }, () => {
console.log(this.state.surNameShow);
});
};
render() {
const { dataSource } = this.state;
const columns = this.columns;
return (
<div>
<p className="mr-3"> Show surname</p>
<Switch onChange={() => this.handleChnage()} />
<Table
bordered
dataSource={dataSource}
columns={
this.state.surNameShow
? columns
: columns.filter(ea => ea.dataIndex !== "surname")
}
pagination={false}
/>
</div>
);
}
}
ReactDOM.render(<EditableTable />, document.getElementById("container"));

React TypeError: Cannot destructure property as it is undefined

Any problem with this code?
class App extends React.Component {
constructor(props) {
super(props);
this.state ={
name: '',
gender: '',
age: '',
};
}
componentWillMount() {
const { steps } = this.props;
const { name, gender, age } =steps;
this.setState({ name, gender,age });
}
the error shows like this :
isn't it defined in the this.state block right above?
Full code here:
App.js
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
name: '',
age: '',
};
}
componentWillMount() {
const { steps } = this.props;
const { name,age } = steps;
this.setState({ name, age });
}
render() {
const { name, age } = this.state;
return (
<div>
<h3>Summary</h3>
<table>
<tbody>
<tr>
<td>Name</td>
<td>{name.value}</td>
</tr>
<tr>
<td>Age</td>
<td>{age.value}</td>
</tr>
</tbody>
</table>
</div>
);
}
}
SimpleForm.js
const steps = [
{
id: '1',
message: 'What is your name?',
trigger: 'name',
},
{
id: 'name',
user: true,
trigger: '5',
},
{
id: '5',
message: 'How old are you?',
trigger: 'age',
},
{
id: 'age',
user: true,
trigger: '7',
},
{
id: '7',
message: 'Great! Check out your summary',
trigger: 'review',
},
{
id: 'review',
component: <App />,
asMessage: true,
end: true,
}
]
class simpleForm extends React.Component {
render() {
return (
<ChatBot steps={steps} />
)
}
}
export default simpleForm;
The error is clear. You're not sending any props to the App component, so { steps } is undefined, and you can't destructure the property "steps" because it's undefined.
componentWillMount is now a deprecated life cycle method and will be removed in version 17. React Documentation.
One option is defining defaults into the state from props.
e.g.
this.state = {
name: this.props.steps.name || '',
gender: this.props.steps.gender || '',
age: this.props.steps.age || ''
}
You could also set it to state after componentDidMount.

React Tabs content

I was playing around with React for the first time and trying to figure out how to make tabs. I found an outline of the code that looks simple and clean enough (do correct me if I'm wrong though, please), but I don't know how to show the content of the tabs outside of the 'constructor'. As I want the tab content to be more than just a couple of words, and the current code seems constricting.
This is the part that I'm talking about:
constructor(props) {
super(props);
this.state = {
activeTabIndex: 0,
initialData: [
{
label: 'Tab 1',
content: 'Content 1',
},
{
label: 'Tab 2',
content: 'Content 2',
},
{
label: 'Tab 3',
content: 'Content 3',
},
],
};
this.handleTabClick = this.handleTabClick.bind(this);
}
How do I pull the content outside of this, if I have too much content to be put inside that? Here is the entire code outline: link. Any suggestions/links are welcome.
And I apologize if I'm explaining this wrong, I'm still trying to figure stuff out. Thanks!
Just create a Component you want to render in Tab Content and assign it to content.
import React, { Component } from "react";
import Tabs from "./Tabs";
import Content from "./Content";
export default class TabView extends Component {
constructor(props) {
super(props);
this.state = {
activeTabIndex: 0,
initialData: [
{
label: "Tab 1",
content: <TabContent />
},
{
label: "Tab 2",
content: "Content 2"
},
{
label: "Tab 3",
content: "Content 3"
}
]
};
this.handleTabClick = this.handleTabClick.bind(this);
}
handleTabClick(index) {
this.setState({
activeTabIndex: index
});
}
render() {
const { initialData, activeTabIndex } = this.state;
const activeItem = this.state.initialData[activeTabIndex];
return (
<div>
<Tabs
handleTabClick={this.handleTabClick}
data={this.state.initialData}
activeTabIndex={activeTabIndex}
/>
<Content content={activeItem.content} />
</div>
);
}
}
const TabContent = () => (
<div>
<p>I am here</p>
</div>
);

Passing data from functional component to app.js

I am new to React, still learning the basics of it.
I have a component in students.js:
export const studentslist = () => {
return [
{
name: "aaaa",
surname: "aaa"
},
{
name: "bbbbb",
surname: "bbbb"
},
{
name: "ccccc",
surname: "ccc"
}
];
};
How can I pass this list of students to my App.js into state and use it?
studentslist is not a component as the function only returns an array of data, but it you want to use what the function returns as initial state of a component, you can import it in the component file and assign it to the state either in the constructor or in a class property.
Example
// studentslist.js
export const studentslist = () => {
return [
{
name: "aaaa",
surname: "aaa"
},
{
name: "bbbbb",
surname: "bbbb"
},
{
name: "ccccc",
surname: "ccc"
}
];
};
// App.js
import React from "react";
import studentslist from "./studentslist";
class App extends React.Component {
state = { studentslist: studentslist() };
render() {
return <div>{JSON.stringify(this.state)}</div>;
}
}
const studentslist = () => {
return [
{
name: "aaaa",
surname: "aaa"
},
{
name: "bbbbb",
surname: "bbbb"
},
{
name: "ccccc",
surname: "ccc"
}
];
};
class App extends React.Component {
state = { studentslist: studentslist() };
render() {
return <div>{JSON.stringify(this.state)}</div>;
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

Resources