I am using TypeScript with react native and when I write my code I got an error which is Property 'selected' does not exist on type '{ id: string; value: string; label: string; color: string; }'.ts(2339) and Type 'string' is not assignable to type '{ id: string; value: string; label: string; color: string; } | undefined'.ts(2322)
my code is:
export default class Instant extends Component {
state = {
one: [
{
id: "a0",
value: "a0",
label: 'A0',
color: 'grey',
checked: false,
},
{
id: "a1",
value: "a1",
label: 'A1',
color: 'grey',
checked: false,
},
{
id: "a2",
value: "a2",
label: 'A2',
color: 'grey',
checked: false,
},
{
id: "a3",
value: "a3",
label: 'A3',
color: 'grey',
checked: false,
},
{
id: "a4",
value: "a4",
label: 'A4',
color: 'grey',
checked: false,
},
{
id: "a5",
value: "a5",
label: 'A5',
color: 'grey',
checked: false,
},
],
data: [
{
id: "Normal",
value: "normal",
label: 'normal',
color: 'grey',
},
{
id: "Gloss",
value: "gloss",
label: 'gloss',
color: 'grey',
},
{
id: "Strong",
value: "strong",
label: 'strong',
color: 'grey',
},
{
id: "Re-used",
value: "reused",
label: 're-used',
color: 'grey',
},
],
two: [
{
id: "Color",
value: "color",
label: 'color',
color: 'grey',
},
{
id: "Black&White",
value: "black&white",
label: 'black and white',
color: 'grey',
},
],
three: [
{
id: "Two-side",
value: "twoside",
label: 'two side',
color: 'grey',
},
{
id: "One-side",
value: "oneside",
label: 'one side',
color: 'grey',
},
],
};
onPress = (data: any) => this.setState({ data });
render() {
let selectedButton = this.state.data.find(s => s.selected == true);
selectedButton = selectedButton ? selectedButton.value : this.state.data[0].label;
The final result must be like that, it works fine but it still have an error on VSCode. enter image description here
this.state.datado indeed not have aselectedproperty. Are they supposed to? Is it optional?selectedstate member that you assign the object to, rather than making it a property of the object.